简体   繁体   中英

How do I create a void * pointer using typeof at runtime

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

The above macro is failing when p is of void ** instead of something else.

It works in c but not in c++. Is there any round-about way to do this in c++?

In C++(11) there is remove_pointer which returns the type pointed to by the input type parameter.

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.

If the cast is all you need a simple function template should help.

template<typename T>
T* remove_pointer_cast(T** p)
{
    return (T*)(p);
}

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