For faster navigation, this Iframe is preloading the Wikiwand page for nullptr.

nullptr

nullptr
基本资料
所属概念空指针
种类字面常量
语言C++
详细资料
语法nullptr
类型std::nullptr_t
头文件nullptr.h (C++11之前)
首次出现C++11

nullptrC++11语言标准用来表示空指针常量值[1],可以指派给任意类型的指针变量[2]。部分编译器将之视为一个关键字,例如Visual Studio[3],部分使用旧标准的C++编译器则未实现需要自行定义[4]或引入额外的头文件[2]

定义

[编辑]

在C语言中,空指针可以使用(void *)0;来表示,且标准库中也是如此定义,但在C++语言中,由于对语法的类型检查更为严格,因而空指针的值就不能表示为(void *)0;。例如,空指针的值表示为FILE *fp=(void *)0;编译报错。所以至少自C++98开始#define NULL 0。 但这会在函数重载时遇到新的困难。例如

void foo(char c, void *p);
void foo(char c, int i);
int main()
{
    foo('x',NULL);//匹配哪个版本的foo()?
}

C++11开始,定义了空指针的常值为nullptr,解决了上述函数重载问题。

在没有C++11的nullptr时,可以自己实现一个[4]

const class nullptr_t
{
public:
    template<class T>
    inline operator T*() const
        { return 0; }

    template<class C, class T>
    inline operator T C::*() const
        { return 0; }
 
private:
    void operator&() const;
} nullptr = {};

C++语言标准规定:[5] 值0或std::nullptr_t类型的纯右值是空指针常量(null pointer constant)。可以通过空指针转换(null pointer conversion)成为某个类型的空指针值(null pointer value)。

C++语言标准还规定,[6]在实参个数多于形参个数时(即可变参数个数的函数调用,可用va_arg来访问),多出来的实参如果是std::nullptr_t类型,则转化为(void *)0供函数访问。

std::nullptr_t定义在标准头文件<cstddef>中。但实际上在源程序中不包含该头文件仍可以正常使用nullptr_t类型与nullptr对象。

nullptr_t

[编辑]

nullptr_t是字面常量nullptr的资料类型[7]。它是一种特殊的类型,并不是一种指针类型也不是指向任何种变量类型的指针类型。nullptr_t位于std命名空间中,且定义于<cstddef>头文件中[8]。可透过is_null_pointer来检查物件是否为这种类型[9]

参见

[编辑]

参考文献

[编辑]
  1. ^ A name for the null pointer: nullptr (PDF). stroustrup.com. 2003-09-10 [2018-09-09]. (原始内容存档 (PDF)于2017-07-05). 
  2. ^ 2.0 2.1 nullptr. Embedded Template Library. [2018-09-09]. (原始内容存档于2018-09-09). 
  3. ^ nullptr (C++ Component Extensions). [2018-09-09]. (原始内容存档于2018-09-09). 
  4. ^ 4.0 4.1 《Imperfect C++ --- Practical Solutions for Real-Life Programming》 作者: 威尔逊 译者: 荣耀 / 刘未鹏 人民邮电出版社 2006年1月 ISBN 9787115136848
  5. ^ 《C++14语言标准》4.10.1
  6. ^ 《C++14语言标准》5.2.2.7
  7. ^ std::nullptr_t. cplusplus.com. [2018-09-09]. (原始内容存档于2018-03-24). 
  8. ^ Null pointers. IBM. [2018-09-09]. (原始内容存档于2018-09-09). 
  9. ^ std::is_null_pointer. cplusplus.com. [2018-09-09]. (原始内容存档于2018-03-12). 
{{bottomLinkPreText}} {{bottomLinkText}}
nullptr
Listen to this article

This browser is not supported by Wikiwand :(
Wikiwand requires a browser with modern capabilities in order to provide you with the best reading experience.
Please download and use one of the following browsers:

This article was just edited, click to reload
This article has been deleted on Wikipedia (Why?)

Back to homepage

Please click Add in the dialog above
Please click Allow in the top-left corner,
then click Install Now in the dialog
Please click Open in the download dialog,
then click Install
Please click the "Downloads" icon in the Safari toolbar, open the first download in the list,
then click Install
{{::$root.activation.text}}

Install Wikiwand

Install on Chrome Install on Firefox
Don't forget to rate us

Tell your friends about Wikiwand!

Gmail Facebook Twitter Link

Enjoying Wikiwand?

Tell your friends and spread the love:
Share on Gmail Share on Facebook Share on Twitter Share on Buffer

Our magic isn't perfect

You can help our automatic cover photo selection by reporting an unsuitable photo.

This photo is visually disturbing This photo is not a good choice

Thank you for helping!


Your input will affect cover photo selection, along with input from other users.

X

Get ready for Wikiwand 2.0 🎉! the new version arrives on September 1st! Don't want to wait?