Significance of nullptr

can anybody please tell me the significance of the origin of the name nullptr in c++11 because the same could be done before using 0 or NULL i.e. for example:

1
2
3
4
5
6
7
8
9
10
double* p0 = nullptr

  if(p0)
//is same as
if(p0 !=0)
//is same as(?)
if(p0 !=NULL)
//is same as(?)
if(p0 != nullptr)


Thanks!
It has type. A 0 can represent integer, float, char, pointer, ... but nullptr has its type. Therefore, the compiler can be much more strict about type correctness, and catch some unintended implicit conversions (i.e. errors).
I did
1
2
3

std::cout << typeid(nullptr).name() << '\n';


it shows this as output:

1
2
3

Dn


what does dn here stand for ?
thanks!
The typeid is not required to produce a value that makes sense to humans, only that it uniquely identify a type.
keskiverto pointed out
nullptr has a type
…what type is it ?

infact all 3 have different type ! :


1
2
3
4
    std::cout << typeid(0).name() << '\n';
    std::cout << typeid(nullptr).name() << '\n';
    std::cout << typeid(NULL).name() << '\n';


1
2
3
i 
Dn
l


i is int and l is long..but what is Dn?
Last edited on
> …what type is it ?

std::nullptr_t http://en.cppreference.com/w/cpp/types/nullptr_t

0 and NULL as pointers just don't work with forwarding templates:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <utility>

template < typename CALLABLE, typename POINTER >
void do_call( CALLABLE fn, POINTER ptr ) { fn(ptr) ; }

template < typename CALLABLE, typename POINTER >
void do_call_perfect( CALLABLE&& fn, POINTER&& ptr )
{ std::forward<CALLABLE>(fn)( std::forward<POINTER>(ptr) ) ; }

void foo( int* ) {}

int main()
{
   do_call( foo, 0 ) ; // *** error: POINTER deduced as int
   do_call_perfect( foo, 0 ) ; // *** also error

   do_call( foo, NULL ) ; // *** error:
   do_call_perfect( foo, NULL ) ; // *** also error

   do_call( foo, nullptr ) ; // fine: POINTER deduced as std::nullptr_t
   do_call_perfect( foo, nullptr ) ; // also fine

}
Last edited on
What keskiverto meant was that nullptr is specifically for pointer(of any type) only.
Imagime if you have function overloads, one taking an int, and the other a pointer say char*. If you are to pass NULL as an argument to the pointer overload, the compiler will select the int overload.

Aceix.
@vxk, to make sense of your compiler's output, pipe it throgh c++filt -t
Last edited on
Topic archived. No new replies allowed.