Power Programming Languages

Power Programming Languages

IBM Power, including the AIX, IBM i, and Linux operating systems, support a wide range of programming languages, catering to both traditional enterprise applications and modern development needs.


#Power


#Servers
#Programminglanguages
 View Only

nullptr in C++11

By Archive User posted 09/16/14 05:00 AM

  

Originally posted by: FangLu


The C++11 standard introduced a new keyword, nullptr as a null pointer constant. The nullptr constant can be distinguished from integer 0 for overloaded functions. The constants of 0 and NULL are treated as of the integer type for overloaded functions, whereas nullptr can be implicitly converted to only the pointer type, pointer-to-member type, and bool type.

Before C++11, initializing null pointers with 0 or NULL makes it impossible to distinguish between a null pointer and integer 0 for overloaded functions. For example, see the following two overloaded functions:

void func(int n);

void func(char *s);

When you call the overloaded function with the following statement:

func( NULL );

Can you guess which function gets called? You might think that func(char *s) should be called, because the actual parameter NULL looks like a pointer. However, surprisingly, the call func(NULL) resolves to func(int), because NULL is a macro and equal to the integer 0, which is of the integral type instead of the pointer type.  

To solve the problems of null pointer constants, C++11 introduced a new keyword nullptr. The nullptr constant can be distinguished from integer 0 for overloaded functions. For example, if you call the previous overloaded function with the following statement:

func( nullptr );

The func(char *s) function will be called.

A null pointer constant with the nullptr value has the following characteristics:

  • It can be converted to any pointer or pointer-to-member type.
  • It cannot be implicitly converted to any other type, except for the bool type.
  • It cannot be used in an arithmetic expression.
  • It can be compared with the integer 0.
  • It can be used in relational expressions to compare with pointers or data of the std::nullptr_t type.

It should be noted that in C++11 it is still acceptable to assign the value 0 or NULL to a pointer.


#C/C++-compilers-for-AIX
#c++11
#C/C++andFortran
0 comments
1 view

Permalink