Namespaces
Variants

std:: is_null_pointer

From cppreference.net
Metaprogramming library
Type traits
Type categories
(C++11)
is_null_pointer
(C++11) ( DR* )
Type properties
(C++11)
(C++11)
(C++14)
(C++11) (deprecated in C++26)
(C++11) ( until C++20* )
(C++11) (deprecated in C++20)
(C++11)
Type trait constants
Metafunctions
(C++17)
Supported operations
Relationships and property queries
Type modifications
Type transformations
(C++11) (deprecated in C++23)
(C++11) (deprecated in C++23)
(C++11)
(C++11) ( until C++20* ) (C++17)

Compile-time rational arithmetic
Compile-time integer sequences
定义于头文件 <type_traits>
template < class T >
struct is_null_pointer ;
(C++11 起)

std::is_null_pointer 是一个 UnaryTypeTrait

检查 T 是否为类型 std::nullptr_t

提供成员常量 value ,当 T 是类型 std::nullptr_t const std:: nullptr_t volatile std:: nullptr_t const volatile std:: nullptr_t 时,该常量等于 true

否则, value 等于 false

如果程序为 std::is_null_pointer std::is_null_pointer_v (since C++17) 添加特化,则行为未定义。

目录

模板参数

T - 待检查的类型

辅助变量模板

template < class T >
constexpr bool is_null_pointer_v = is_null_pointer < T > :: value ;
(自 C++17 起)

继承自 std:: integral_constant

成员常量

value
[static]
T 是类型 std::nullptr_t (可能带有 cv 限定符)则为 true ,否则为 false
(公开静态成员常量)

成员函数

operator bool
转换对象为 bool 类型,返回 value
(公开成员函数)
operator()
(C++14)
返回 value
(公开成员函数)

成员类型

类型 定义
value_type bool
type std:: integral_constant < bool , value >

可能的实现

template<class T>
struct is_null_pointer : std::is_same<std::nullptr_t, std::remove_cv_t<T>> {};

注释

std::is_pointer 对于 std::nullptr_t 返回 false ,因为它不是内置指针类型。

在 libc++ 中, std::is_null_pointer 在 C++11 模式下不可用。

功能测试 标准 功能
__cpp_lib_is_null_pointer 201309L (C++14)
(DR11)
std::is_null_pointer

示例

#include <type_traits>
static_assert(std::is_null_pointer_v<decltype(nullptr)>);
static_assert(!std::is_null_pointer_v<int*>);
static_assert(!std::is_pointer_v<decltype(nullptr)>);
static_assert(std::is_pointer_v<int*>);
int main()
{
}

缺陷报告

以下行为变更缺陷报告被追溯应用于先前发布的C++标准。

DR 适用范围 发布时的行为 正确行为
LWG 2247 C++11 用于检测 std::nullptr_t 的类型特征缺失 已添加

参见

(C++11)
检查类型是否为 void
(类模板)
(C++11)
检查类型是否为数组类型
(类模板)
(C++11)
检查类型是否为指针类型
(类模板)
(C++11)
检查类型是否为枚举类型
(类模板)
(C++11)
检查类型是否为联合体类型
(类模板)
(C++11)
检查类型是否为非联合体类类型
(类模板)
检查类型是否为函数类型
(类模板)
(C++11)
检查类型是否为对象类型
(类模板)