c++ - 如何在运行时使用 typeof 创建一个 void * 指针

标签 c++

#define test(p) (typeof(*(*p)) *)

pvoid ** 而不是其他内容时,上述宏将失败。

它在 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/

相关文章:

c++ - 获取 llvm::Function 静态地址

c++ - boost 随机生成器返回相同的值

从字符串中删除括号的 C++ 函数并不能全部捕获

c++ - 构造函数重载和 SFINAE

c++ - 整数到 double 的转换

c++ - 创建 IloObjective 的正确方法

c++ - C++ 中的抽象矩阵类

c++ - 使用 std::vector 的奇怪段错误

c++ - OpenCV训练错误(人脸识别)

c++ - 如何计算函数的搜索复杂度