C++ Tutorial: Enhance Type Safety and Code Clarity with the nullptr Keyword
来源:互联网 发布:js鼠标滚轮控制滚动条 编辑:程序博客网 时间:2024/04/29 16:28
Introduction
The literal 0 has a unique position in C++ 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 in C++ programming is merely a synonym for 0:
f(NULL); //calls f(int)!
C usually defines NULL as (void*)(0). Many compilers don't even issue a warning in this case. The bug is detected (if ever) only at crash-time.
In contemporary C++ 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 many years. 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 an rvalue
constant 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
Many programming languages 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 many years. 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.
- C++ Tutorial: Enhance Type Safety and Code Clarity with the nullptr Keyword
- Thread-safety with the Java final keyword
- Thread-safety with the Java final keyword
- NLP keyword extraction tutorial with RAKE and Maui
- Thread Safety Using the Volatile Keyword
- Refactor: replace type code with class (c++)
- 解决Type safety: The expression of type List needs
- The RS232 Standard --- Tutorial with Signal Name and Definition
- Safe! Repel Attacks on Your Code with the Visual Studio 2005 Safe C and C++ Libraries
- Using the C "volatile" Keyword
- Something interesting with the keyword final
- Incorrect syntax near the keyword 'WITH'
- Linking LAPACK and BLAS libraries with Fortran and C code
- Java 警告------Type safety: The method add(Object) belongs to the raw type Vector
- Type safety: The method add(Object) belongs to the raw type ArrayList
- The Power of Clarity (ZT)
- Standard iostreams ---Type safety
- java 5.0 Type safety
- 如何使用kobold2d来做一个简单的iphone游戏教程(第四部分)
- JUnit in Action Note Taking (2nd)
- 让调试利器zend debugger与zend Optimizer共存,和谐!
- DB2 建索引SQL
- c++ const函数精解(国外大学的教程就是透彻)
- C++ Tutorial: Enhance Type Safety and Code Clarity with the nullptr Keyword
- Zend Debugger安装
- myeclipse 配置多个tomcat
- 如何查看进程 IO 读写情况?
- ArrayCollection更新单个对象数据
- 游标使用
- HTML进度条
- 展讯平台-sensor驱动
- socket 发送与接收结构体