I read about std::is_pointer
in C++.
Then I wrote the program and check whether T
is a pointer type or not using std::is_pointer
.
#include <iostream>
int main()
{
std::cout<<std::boolalpha;
std::cout<<"char : " <<std::is_pointer<char>::value<<std::endl; // OK
std::cout<<"char * : "<<std::is_pointer<char *>::value<<std::endl; // OK
std::cout<<"char ** : "<<std::is_pointer<char **>::value<<std::endl; // OK
std::cout<<"char *** : "<<std::is_pointer<char ***>::value<<std::endl; // OK
std::cout<<"std::nullptr_t : "<<std::is_pointer<std::nullptr_t>::value<<std::endl; // Not ok, Why false??
}
Output : [Wandbox Demo]
char : false
char * : true
char ** : true
char *** : true
std::nullptr_t : false
Why is std::is_pointer<std::nullptr_t>::value
equal to false
?
Because std::nullptr_t
is not a pointer type. And is_pointer
only evaluates to true
for pointer types.
nullptr_t
is convertible to a pointer, but it isn't a pointer. Indeed, nullptr_t
is not a class type, integral, floating-point, enumerator, or any kind of type other than is_null_pointer
type. It has its own unique classification in the categorization of types.
As unintuitive as it sounds, std::nullptr_t
is not a pointer type. For instance, you cannot dereference a std::nullptr_t
, so it'd be pretty weird if is_pointer_type<nullptr_t>::value
was true
.
It is merely convertible to any pointer type.
Because it's not a pointer type. It's a distinct type that is implicitly convertible to any pointer type.
As the note in [lex.nullptr] summarizes:
The pointer literal is the keyword nullptr. It is a prvalue of type std::nullptr_t. [ Note: std::nullptr_t is a distinct type that is neither a pointer type nor a pointer to member type; rather, a prvalue of this type is a null pointer constant and can be converted to a null pointer value or null member pointer value. See [conv.ptr] and [conv.mem]. — end note ]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With