I”m trying to find a way to tell if a type is a pointer at compile time. That is something like this:
#include <type_traits>
#if std::is_pointer<char*>::value
#pragma message("blah")
#endif
However, this gives “warning C4067: unexpected tokens following preprocessor directive – expected a newline” twice (I think the :: is what confuses it) and it doesn’t print blah. When I hoover over ::value the compiler tells me if it’s true, which means it’s known at compile time so this should work.
The reason for this is that I want to be able to do something like this:
T pHead;
#if std::is_pointer<T>::value
pHead= NULL;
#endif
where I NULL the variable if it’s a pointer. It has to be a compile time check because if T is a struct I cannot assign NULL to its variable. I.e. the following code won’t compile when T is a struct:
T pHead;
if (std::is_pointer<T>::value)
pHead= NULL;
Thanks
Matt
You can initialize the variable to its default value, which is NULL for a pointer type.
Works for most structs as well.