首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::nullptr_t

Defined in header <cstddef>

typedef decltype(nullptr) nullptr_t;

(since C++11)

std::nullptr_t为空指针文字的类型,nullptr它本身不是指针类型或成员类型的指针。

如果两个或多个重载接受不同的指针类型,则std::nullptr_t必须接受空指针参数。

二次

代码语言:javascript
复制
#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);  // would be ambiguous without void f(nullptr_t)
    // f(NULL);  // ambiguous overload: all three functions are candidates
}

二次

产出:

二次

代码语言:javascript
复制
Pointer to integer overload
Pointer to double overload
null pointer overload

二次

另见

nullptr

the pointer literal which specifies a null pointer value (C++11)

NULL

implementation-defined null pointer constant (macro constant)

is_null_pointer (C++14)

checks if a type is std::nullptr_t (class template)

代码语言:txt
复制
 © cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券