In C, it's not an error to cast pointers to and from void *
.
A major obstacle in porting to C++ is the need to cast pointers when returning from functions dealing with generic pointers such as malloc
, and functions declared in my own code such as void *block_get(Blkno const blkno);
.
My code however is intended to be compiled by C and C++ compilers successfully. If I provide explicit casts everywhere for the sake of C++, they must be C-style casts and I may be masking bugs due to casting non-pointer types to and from pointer types from both languages.
My reference error is the following:
struct Cpfs *cpfs = calloc(1, sizeof(*cpfs));
which in MSVC produces:
Error 2 error C2440: 'initializing' : cannot convert from 'void *' to 'Cpfs *' e:\src\cpfs\cpfs.c 179
Evidently I can't use new
or static_cast
which I'd naturally use if I was no longer using C. What's the best way to provide maximum type safety surrounding void *
for each language with minimal verbosity?