[C ++ 11] new feature-introducing nullprt

Source: Internet
Author: User
Tags define null

1. Reasons for introducing nullptr
The reason for introducing nullptr is that it should start with NULL. For C and C ++ programmers, NULL is certainly not unfamiliar. However, NULL in C and C ++ is not equivalent. NULL indicates that the pointer does not point to any object, but the problem is that NULL is not a keyword, but a macro definition (macro ).

1.1 Definition of NULL in C
In C, NULL is often defined as void * pointer value 0:
[Cpp]
# Define NULL (void *) 0

# Define NULL (void *) 0. At the same time, NULL can be defined as an integer 0.

1.2 Definition of NULL in C ++
In C ++, NULL is clearly defined as an integer constant 0:
[Cpp]
// The source code for defining NULL in lmcons. h
# Ifndef NULL
# Ifdef _ cplusplus
# Define NULL 0
# Else
# Define NULL (void *) 0)
# Endif
# Endif

// The source code for defining NULL in lmcons. h
# Ifndef NULL
# Ifdef _ cplusplus
# Define NULL 0
# Else
# Define NULL (void *) 0)
# Endif
# Endif1.3 why is C ++ not fully compatible with C in NULL?
The root cause is related to the overload function of C ++. C ++ tries to find the best-match function by searching for matching parameters. If implicit type conversion of void * is supported, syntax ambiguous.
[Cpp]
// Consider the following two overloaded functions
Void foo (int I );
Void foo (char * p)
 
Foo (NULL); // which is called?

// Consider the following two overloaded functions
Void foo (int I );
Void foo (char * p)

Foo (NULL); // which is called? 2. Application scenarios of nullptr
2.1 Compiler
If our compiler supports nullptr, we should directly use nullptr to replace the macro definition of NULL. They are completely equivalent during normal use.
For compilers, Visual Studio 2010 has begun to support most of the features in C ++ 0x, including nullptr. This keyword is not supported in versions earlier than VS2010.
The G ++ 4.4.1 attached to Codeblocks10.5 does not support nullptr. After being upgraded to 4.6.1, nullptr is supported (the-std = c ++ 0x compilation option must be enabled)

2.2 usage
0 (NULL) and nullptr can be used in exchange, as shown in the following example:
[Cpp]
Int * p1 = 0;
Int * p2 = nullptr;
 
If (p1 = 0 ){}
If (p2 = 0 ){}
If (p1 = nullptr ){}
If (p2 = nullptr ){}
If (p1 = p2 ){}
If (p2 ){}

Int * p1 = 0;
Int * p2 = nullptr;

If (p1 = 0 ){}
If (p2 = 0 ){}
If (p1 = nullptr ){}
If (p2 = nullptr ){}
If (p1 = p2 ){}
If (p2) {} cannot assign nullptr to an integer, as shown in the following example:
[Cpp]
Int n1 = 0; // OK
Int n2 = nullptr; // error
 
If (n1 = nullptr) {}// error
If (n2 = nullptr) {}// error
If (nullprt) {}// error
Nullptr = 0 // error

Int n1 = 0; // OK
Int n2 = nullptr; // error

If (n1 = nullptr) {}// error
If (n2 = nullptr) {}// error
If (nullprt) {}// error
Nullptr = 0 // error the overload problem mentioned above. When nullptr is used, char * is called *.
[Cpp]
Void foo (int) {cout <"int" <endl ;}
Void foo (char *) {cout <"pointer" <endl ;}
 
Foo (0); // callfoo (int)
Foo (nullptr); // callfoo (char *)

Void foo (int) {cout <"int" <endl ;}
Void foo (char *) {cout <"pointer" <endl ;}

Foo (0); // callfoo (int)
Foo (nullptr); // callfoo (char *) 3. Simulate nullptr implementation
Some compilers do not support nullptr, The New Keyword of c ++ 11. We can simulate and implement a nullptr.
[Cpp]
Const
Class nullptr_t_t
{
Public:
Template <class T> operator T * () const {return 0 ;}
Template <class C, class T> operator t c: * () const {return 0 ;}
Private:
Void operator & () const;
} Nullptr_t = {};
# Undef NULL
# Define NULL nullptr_t

Const
Class nullptr_t_t
{
Public:
Template <class T> operator T * () const {return 0 ;}
Template <class C, class T> operator t c: * () const {return 0 ;}
Private:
Void operator & () const;
} Nullptr_t = {};
# Undef NULL
# Define NULL nullptr_t

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.