在 C++ 中放松 void * 铸造
在 C 中,将指针转换为 void *
或从 void *
转换指针并不是错误。
移植到 C++ 的一个主要障碍是,从处理通用指针(例如 malloc)的函数返回时需要强制转换指针,以及在我自己的代码中声明的函数(例如 void *block_get(Blkno const) blkno);。
然而,我的代码旨在由 C 和 C++ 编译器成功编译。如果我为了 C++ 的缘故而在各处提供显式强制转换,那么它们一定是 C 风格的强制转换,并且我可能会掩盖由于将非指针类型与两种语言的指针类型进行强制转换而产生的错误。
我的参考错误如下:
struct Cpfs *cpfs = calloc(1, sizeof(*cpfs));
在 MSVC 中产生:
错误 2 错误 C2440:“初始化”:无法从“void *”转换为“Cpfs *”e:\src\cpfs\cpfs.c 179
显然我无法使用 new
或 static_cast
如果我不再使用 C,我自然会使用它。以最少的冗长为每种语言提供围绕 void *
的最大类型安全性的最佳方法是什么?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我建议要么简单地使用 C 风格的转换,要么将转换包装在一个宏中,该宏要么扩展为空(在 C 中),要么在 C++ 中扩展为
static_cast
。I'd suggest either simply using C style casts, or wrapping the cast in a macro that either expands to nothing (in C), or a
static_cast
in C++.如果您的编译器支持
decltype()
,您可以使用一些宏魔法来避免必须显式重复类型名称(并且,由于sizeof
,元素大小):示例用法:
更干净的解决方案可能是只使用 C 编译器并链接目标文件,不过......
If your compiler supports
decltype()
, you can use some macro magic to avoid having to explicitly repeat the type name (and, thanks tosizeof
, the element size):Example usage:
The cleaner solution would probably be to just use a C compiler and link the object files, though...
创建一个替换分配器函数,您可以为 C 和 C++ 构建进行不同的定义:- 在头文件中类似这样的内容:
现在,在 C++ 构建中,您拥有一个可以推断其处理的事物类型的函数,在 C 构建中,它是一个返回 void* 的常规函数。
唯一的问题是需要传入要分配的指针 - c++ 编译器不会尝试仅根据函数的返回类型推断模板参数。所以你可以像这样不可知论地称呼它:-
make a replacement allocator function that you can define differently for C and C++ builds :- Something like this in a header file:
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 :-
也许是这样的? (未经测试,没有可用的编译器,不经常使用宏):
Maybe something like this? (untested, no compiler available, not using macros very often):
我知道的唯一解决方案是进行显式转换:
这里两个编译器都满意。
另请记住,对于较旧的编译器,malloc 可能会返回 char*。
马里奥
The only solution I know is to do explicit casting:
Here both compilers are satisfied.
Also that remember, that for older compilers malloc may return char*.
hth
Mario