std::nullptr_t

定义于头文件 <cstddef>
typedef decltype(nullptr) nullptr_t;
(C++11 起)

std::nullptr_t 是空指针字面量 nullptr 的类型。它是既非指针类型亦非指向成员指针类型的独立类型。

示例

若二个重载接受不同指针类型,则需要 std::nullptr_t 的重载以接受空指针常量。

#include <cstddef>
#include <iostream>
 
void f(int* pi)
{
   std::cout << "Pointer to integer overload\n";
}
 
void f(double* pd)
{
   std::cout << "Pointer to double overload\n";
}
 
void f(std::nullptr_t nullp)
{
   std::cout << "null pointer overload\n";
}
 
int main()
{
    int* pi; double* pd;
 
    f(pi);
    f(pd);
    f(nullptr);  // 无 void f(nullptr_t) 可能有歧义
    // f(0);  // 歧义调用:三个函数全部为候选
    // f(NULL); // 若 NULL 是整数空指针常量则为歧义
                // (如在大部分实现中的情况)
}

输出:

Pointer to integer overload
Pointer to double overload
null pointer overload

参阅

nullptr 指定空指针值的指针字面量(C++11)
实现定义的空指针常量
(宏常量)
检查类型是否为 std::nullptr_t
(类模板)

版本历史

  • (当前 | 先前 2017年7月25日 (二) 06:35Fruderica讨论 | 贡献 . . (1,321字节) (+12). . (撤销)
  • 当前 | 先前 2017年7月8日 (六) 23:14Fruderica讨论 | 贡献. . (1,309字节) (-222). . (撤销)
  • 当前 | 先前 2014年10月26日 (日) 18:16P12bot讨论 | 贡献 . . (1,531字节) (0). . (Fix some translations) (撤销)
  • 当前 | 先前 2013年7月2日 (二) 11:43P12bot讨论 | 贡献 . . (1,531字节) (-80). . (Use {{lc}}. Update links. Various fixes.) (撤销)
  • 当前 | 先前 2012年11月2日 (五) 15:41P12bot讨论 | 贡献 . . (1,611字节) (+217). . (r2.7.3) (机器人添加:de, en, es, fr, it, ja, pt, ru) (撤销)
  • 当前 | 先前 2012年10月27日 (六) 11:50P12讨论 | 贡献 . . (1,394字节) (0). . (1个修订: Translate from the English version) (撤销)
  • 当前 | 先前 2012年10月26日 (五) 06:00TranslationBot讨论 | 贡献. . (1,394字节) (-19). . (Translated from the English version using Google Translate) (撤销)
  • 当前 | 先前 2012年10月26日 (五) 03:34P12讨论 | 贡献 . . (1,413字节) (0). . (1个修订: Translate from the English version) (撤销)
  • 当前 | 先前) 2012年10月25日 (四) 12:00TranslationBot讨论 | 贡献. . (1,413字节) (+1,413). . (Translated from the English version using Google Translate)