简体   繁体   中英

Relax void * casting in C++

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?

我建议要么简单地使用 C 样式转换,要么将转换包装在一个宏中,该宏要么扩展为空(在 C 中),要么在 C++ 中使用static_cast

If your compiler supports decltype() , you can use some macro magic to avoid having to explicitly repeat the type name (and, thanks to sizeof , the element size):

#ifdef __cplusplus
#define my_calloc(VAR, COUNT) \
    static_cast<decltype(VAR)>(std::calloc(COUNT, sizeof *VAR))
#else
#define my_calloc(VAR, COUNT) calloc(COUNT, sizeof *VAR)
#endif

Example usage:

#ifdef __cplusplus
#include <cstdlib>
#else
#include <stdlib.h>
#endif

struct Cpfs *cpfs = my_calloc(cpfs, 42);

The cleaner solution would probably be to just use a C compiler and link the object files, though...

make a replacement allocator function that you can define differently for C and C++ builds :- Something like this in a header file:

#ifdef __cplusplus
template<typename TypeT>
TypeT* MyAlloc(TypeT** pOut,size_t cb){
  *pOut = static_cast<TypeT*>(malloc(cb)); //aint c++ pretty.
  return *pOut;
}
#else
  extern void* MyAlloc(void** ppv, size_t cb);
#endif

Now you have, in c++ builds, a function that can infer the type of thing its dealing with, and in C builds, its a regular function that returns a void*.

The only problem is the need to pass in the pointer to allocate - the c++ compiler wont try to deduce a template parameter based only on the return type of a function afaik. So you could call it agnostically like this :-

int *p;
if(MyAlloc(&p,sizeof(int)*n)){
  ...

The only solution I know is to do explicit casting:

struct Cpfs *cpfs = (Cpfs*)calloc(1, sizeof(*cpfs));

Here both compilers are satisfied. Also that remember, that for older compilers malloc may return char*.

hth

Mario

Maybe something like this? (untested, no compiler available, not using macros very often):

#ifdef __cplusplus
    #define pointer_cast(type, pointer) reinterpret_cast<type>(pointer)
#else
    #define pointer_cast(type, pointer) (type)(pointer)
#endif

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM