[英]Relax void * casting in C++
In C, it's not an error to cast pointers to and from void *
.在 C 中,将指针转换为
void *
和从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);
移植到 C++ 的一个主要障碍是从处理通用指针的函数返回时需要转换指针,例如
malloc
,以及在我自己的代码中声明的函数,例如void *block_get(Blkno const blkno);
. .
My code however is intended to be compiled by C and C++ compilers successfully.然而,我的代码旨在由 C和C++ 编译器成功编译。 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.
如果我为了 C++ 的缘故在任何地方都提供显式强制转换,那么它们必须是 C 风格的强制转换,并且由于将非指针类型转换为来自两种语言的指针类型和从两种语言的指针类型转换,我可能会掩盖错误。
My reference error is the following:我的参考错误如下:
struct Cpfs *cpfs = calloc(1, sizeof(*cpfs));
which in MSVC produces:在 MSVC 中产生:
Error 2 error C2440: 'initializing' : cannot convert from 'void *' to 'Cpfs *' e:\\src\\cpfs\\cpfs.c 179
错误 2 错误 C2440:“正在初始化”:无法从“void *”转换为“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?显然我不能使用
new
或static_cast
如果我不再使用 C,我自然会使用它们。为每种语言提供围绕void *
最大类型安全性的最佳方法是什么?
我建议要么简单地使用 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):如果您的编译器支持
decltype()
,您可以使用一些宏魔法来避免显式重复类型名称(并且,感谢sizeof
,元素大小):
#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...更简洁的解决方案可能是只使用 C 编译器并链接目标文件,尽管......
make a replacement allocator function that you can define differently for C and C++ builds :- Something like this in a header file:制作一个替换分配器函数,您可以为 C 和 C++ 构建定义不同的分配器函数:- 在头文件中是这样的:
#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*.现在,在 C++ 构建中,您拥有一个可以推断其处理对象类型的函数,而在 C 构建中,它是一个返回 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.唯一的问题是需要传入要分配的指针 - C++ 编译器不会尝试仅根据函数的返回类型推断模板参数 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*.
还要记住,对于较旧的编译器,malloc 可能会返回 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.