#define test(p) (typeof(*(*p)) *)
当 p
为 void **
而不是其他内容时,上述宏将失败。
它在 c 中有效,但在 c++ 中无效。在 C++ 中有什么迂回的方法可以做到这一点吗?
最佳答案
在 C++(11) 中有 remove_pointer它返回输入类型参数指向的类型。
Basically, when p is int **, I use that macro to do int * type casting. But it fails with error when p is void **.[...] No, I cant use typeof(*p), because this macro also need to ensure that p is a double pointer.
如果您只需要强制转换,一个简单的函数模板应该会有所帮助。
template<typename T>
T* remove_pointer_cast(T** p)
{
return (T*)(p);
}
关于c++ - 如何在运行时使用 typeof 创建一个 void * 指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13818066/