Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is "std::is_pointer<std::nullptr_t>::value" equal to false?

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?

like image 223
msc Avatar asked Nov 14 '17 05:11

msc


3 Answers

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.

like image 120
Nicol Bolas Avatar answered Oct 18 '22 14:10

Nicol Bolas


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.

like image 23
zneak Avatar answered Oct 18 '22 15:10

zneak


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 ]

like image 31
StoryTeller - Unslander Monica Avatar answered Oct 18 '22 15:10

StoryTeller - Unslander Monica