C ++ Tutorial: Enhance type safety and code clarity with the nullptr keyword

Source: Internet
Author: User

Introduction

The literal 0 has a unique position inC ++ Programming-- It automatically converts to almost every fundamental type, depending on the context:

  int x=0;  double d=0;  char * pstr=0; //null pointer  int (A::*pmf)(int) =0; //null pointer to member  bool flag=0; //Boolean false

Using 0 as a null pointer might lead to overload resolution bugs such as this:

 void f(int); //#1  void f(char *);//#2    f(0); //which f is called?

The C ++ programmer believes that #2 is called. however, the compiler interprets 0 in this context as an integer, never as a pointer, and selects #1 instead. things get worse when the macro null is used. recall that null inC ++ ProgrammingIs
Merely a synonym for 0:

  f(NULL); //calls f(int)!

C usually defines null as (void *) (0). Compile compilers don't even issue a warning in this case. The bug is detected (if ever) only at Crash-time.

In contemporaryC ++ Programming, The only workaround is an explicit typecast expression that forces the compiler to select the desired function:

  f((char *)0); //unambiguous but still a kludge

This workaround isn' t ideal though. often, programmers don't use an explicit cast expression because they're not aware of the problem. additionally, the cast expression obfuscates the code and makes it less readable.

Using nullptr

The C ++ Standards Committee has been aware of this problem for policyears. however, a consensus about the right solution was reached only recently in the form of a new keyword that designated a null pointer value. nullptr is a C ++ 0x reserved keyword designating
AnrvalueConstant of Typestd::nullptr_t.

Nullptr is implicitly converted to pointers and pointers to members. However, it's not convertible to bool or any of the numeric types of C ++.

You can use nullptr as an initializer for all pointer types and in comparison expressions:

  const char *pc=str.c_str();  if (pc!=nullptr)    cout<<pc<<endl;  int (A::*pmf)()=nullptr; //pointer to member function  int A::*pmi=nullptr; //pointer to data member 

Any attempt to use nullptr as a non-pointer type will cause a compilation error:

  char* ps=nullptr; //OK  char c=nullptr; //error, type mismatch  int n=nullptr; //error, type mismatch

A solution to a thorny problem

The use of nullptr eliminates the overload resolution problem:

  func(nullptr); //calls func (char*)  func(0); //calls func(int)

You may explicitly instantiate objects of Type STD: nullptr_t, and use nullptr in sizeof expressions:

  cout<< typeid(nullptr).name();   size_t sz= sizeof(nullptr); 

Throwing nullptr_t objects from a try block is also allowed:

  try  {   if(failure)     throw nullptr; //OK  }  catch(std::nullptr_t)   {   cerr<<"terminating!";  }

Conclusion

Extends programming into ages have used a reserved keyword to designate a null pointer or a null reference for ages. the lack of a similar keyword in C ++ programming has been a fertile source of bugs for policyears. the addition of nullptr finally fixes a this
Longstanding loophole in the Type System of C ++. Microsoft's Visual Studio 2010 already
Supports this feature and so does GCC 4.6; other vendors will also implement this feature soon.

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.