0.NULL的前世今生
对于C和C++程序员来说,一定不会对NULL感到陌生。但是C和C++中的NULL却不等价(别惊讶,这是真的)。NULL表示指针不指向任何对象,但是问题在于,NULL不是关键字,而只是一个宏定义(macro)。在C中,习惯将NULL定义为void*指针值0:
#define NULL (void*)0
但同时,也允许将NULL定义为整常数0
An integer constant expression with the value 0, or such an expression cast to type void *, is called a null pointer constant.55) If a null pointer constant is converted to a pointer type, the resulting pointer, called a null pointer, is guaranteed to compare unequal to a pointer to any object or function.
在C++中,NULL却被明确定义为整常数0:
#define NULL 0
A null pointer constant is an integral constant expression (5.19) rvalue of integer type that evaluates to zero.
导致不同定义的根本原因是:隐式类型转换(implicit conversion)。C的定义使得存在void*到0的隐式类型转换,而宏观表现则是,void*指针可以直接赋值给其他的指针类型
int*ptr = 0;
int*pptr = (void*)0;// both are legal in c
但是C++却不支持void*到其他类型的隐式转换,所以上面的代码在C++中并不能全部通过编译。
1.C++的解释
为什么C++在NULL上选择不完全兼容C?原因和C++的重载函数有关。C++通过搜索匹配参数的机制,试图找到最佳匹配(best-match)的函数,而如果继续支持void*的隐式类型转换,则会带来语义二义性(syntax ambiguous)的问题。考虑下面两个重载函数:
voidfoo(inti); voidfoo(char*p)
foo(NULL);// which is called? |
编译器同时找到了两个最佳匹配,以至于连编译器都不知道选择哪个。为了消除混乱,C++才会将NULL定义为整常数0。但是,这么做又带来了另一个问题:NULL和整型发生了重叠。
2.C++的选择
为了彻底解决NULL带来的问题,C++11标准引入了一个新的关键字:nullptr。nullptr是一个很神奇的关键字,他被定义为std::nullptr_t类型,这是一个泛指针类型(这个术语是我自己加的-.-),支持到其他指针类型的自动转换。但不支持到除了bool类型以外的整型的转换。
由于nullptr具有类型,且支持到任何兼容的指针类型的转换,所以对于上述重载函数的调用,编译器会很明确的选择第二个函数。不过,引入新关键字并不代表解决了一切问题。其代价也很明显:向后兼容性/习惯性和编译器的支持程度。
C++11中保留了原来的NULL,所以旧的代码也可以通过编译。并且为了和C保持最大程度的兼容,在一段时间内,NULL都应该是支持的。至于程序员对于nullptr的接受程度,那只能另当别论。
对于编译器,Visual Studio 2010、gcc 4.8、clang3.3已经开始支持C++11中的大部分特性,自然包括nullptr。考虑到目前的编译环境,用nullptr全面代替NULL不太现实,不过可以在文件头加入说明或使用预编译。
对于老的编译器也可以支持nullptr功能的,通过隐式的类型转换就可以。但需要把nullptr定义成常量。C++11中还有shared_ptr类型,也可以通过重载的方式,实现对shared_ptr的支持,使得shared_ptr更像指针。
const class nullptr_t
{
public:
nullptr_t() {}
// convertible to any type of null non-member pointer...
template<typename T> operator T* () const{ return (T*)0; }
//or any type of null member pointer...
template<typename C, typename T> operator T C::* () const { return (T C::*)0; }
// support operator overloading (== and !=)
template<typename T> bool equals(T const& rhs) const { return rhs == 0; }
//convertible to any type of null non-member pointer...
template<typename T> operator shared_ptr<T> () const{ return shared_ptr( (T*)0 ); }
private:
// can't take address of nullptr
void operator&() const;
// can't copyable
nullptr_t(const nullptr_t&);
const nullptr_t& operator=(const nullptr_t&);
} nullptr;
template<typename T> inline bool operator==(const nullptr_t& lhs, T const& rhs) { return lhs.equals(rhs); }
template<typename T> inline bool operator==(T const& lhs, const nullptr_t& rhs) { return rhs.equals(lhs); }
template<typename T> inline bool operator!=(const nullptr_t& lhs, T const& rhs) { return !lhs.equals(rhs); }
template<typename T> inline bool operator!=(T const& lhs, const nullptr_t& rhs) { return !rhs.equals(lhs); }
template<typename T> inline bool operator==(const nullptr_t& lhs, shared_ptr<T> const& rhs) { return lhs.equals(rhs.get()); }
template<typename T> inline bool operator==(shared_ptr<T> const& lhs, const nullptr_t& rhs) { return rhs.equals(lhs.get()); }
template<typename T> inline bool operator!=(const nullptr_t& lhs, shared_ptr<T> const& rhs) { return !lhs.equals(rhs.get()); }
template<typename T> inline bool operator!=(shared_ptr<T> const& lhs, const nullptr_t& rhs) { return !rhs.equals(lhs.get()); }
class A {};
int main()
{
nullptr_t* n2;
n2 = nullptr;
int* p = nullptr;
if (p == nullptr || p != nullptr)
;
if (nullptr == p || nullptr != p)
;
A* a = nullptr;
if (a == nullptr || a != nullptr)
;
if (nullptr == a || nullptr != a)
;
shared_ptr<int> pInt = nullptr;
if( pInt == nullptr || nullptr != pInt )
;
return 0;
}