命名空间
变体
操作

std::is_default_constructible, std::is_trivially_default_constructible, std::is_nothrow_default_constructible

来自 cppreference.cn
< cpp‎ | types
 
 
元编程库
类型特征
类型类别
(C++11)
(C++11)(DR*)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11) 
(C++11)
(C++11)
类型属性
(C++11)
(C++11)
(C++14)
(C++11)(在 C++26 中已弃用)
(C++11)(直到 C++20*)
(C++11)(在 C++20 中已弃用)
(C++11)
类型特征常量
元函数
(C++17)
支持的操作
is_default_constructibleis_trivially_default_constructibleis_nothrow_default_constructible
(C++11)(C++11)(C++11)
关系和属性查询
类型修改
(C++11)(C++11)(C++11)
类型转换
(C++11)(在 C++23 中已弃用)
(C++11)(在 C++23 中已弃用)
(C++11)
(C++11)(直到 C++20*)(C++17)

(C++11)
(C++17)
编译时有理算术
编译时整数序列
 
定义于头文件 <type_traits>
template< class T >
struct is_default_constructible;
(1) (since C++11)
template< class T >
struct is_trivially_default_constructible;
(2) (since C++11)
template< class T >
struct is_nothrow_default_constructible;
(3) (since C++11)
1) 提供成员常量 value 等于 std::is_constructible<T>::value
2) 提供成员常量 value 等于 std::is_trivially_constructible<T>::value
3) 提供成员常量 value 等于 std::is_nothrow_constructible<T>::value

如果 T 不是完整类型,(可能带有 cv 限定符的)void,或者未知边界的数组,则行为未定义。

如果上述模板的实例化直接或间接地依赖于不完整类型,并且如果假设该类型已完成,则该实例化可能会产生不同的结果,则行为未定义。

如果程序为此页面上描述的任何模板添加了特化,则行为未定义。

目录

[编辑] 辅助变量模板

template< class T >

inline constexpr bool is_default_constructible_v =

    is_default_constructible<T>::value;
(since C++17)
template< class T >

inline constexpr bool is_trivially_default_constructible_v =

    is_trivially_default_constructible<T>::value;
(since C++17)
template< class T >

inline constexpr bool is_nothrow_default_constructible_v =

    is_nothrow_default_constructible<T>::value;
(since C++17)

继承自 std::integral_constant

成员常量

value
[静态]
true 如果 T 是默认可构造的,false 否则
(公共静态成员常量)

成员函数

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

成员类型

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

[编辑] 可能的实现

template<class T>
struct is_default_constructible : std::is_constructible<T> {};
 
template<class T>
struct is_trivially_default_constructible : std::is_trivially_constructible<T> {};
 
template<class T>
struct is_nothrow_default_constructible : std::is_nothrow_constructible<T> {};

[编辑] 注释

在许多实现中,std::is_nothrow_default_constructible 还会检查析构函数是否抛出异常,因为它实际上等同于 noexcept(T())std::is_trivially_default_constructible 也是如此,在这些实现中,它还要求析构函数是平凡的:GCC bug 51452, LWG issue 2116

std::is_default_constructible<T> 不测试 T x; 是否会编译;它尝试使用空参数列表的直接初始化(参见 std::is_constructible)。因此, std::is_default_constructible_v<const int>std::is_default_constructible_v<const int[10]>true

[编辑] 示例

#include <string>
#include <type_traits>
 
struct S1
{
    std::string str; // member has a non-trivial default constructor
};
static_assert(std::is_default_constructible_v<S1> == true);
static_assert(std::is_trivially_default_constructible_v<S1> == false);
 
struct S2
{
    int n;
    S2() = default; // trivial and non-throwing
};
static_assert(std::is_trivially_default_constructible_v<S2> == true);
static_assert(std::is_nothrow_default_constructible_v<S2> == true);
 
int main() {}

[编辑] 参见

检查类型是否具有用于特定参数的构造函数
(类模板) [编辑]
检查类型是否具有复制构造函数
(类模板) [编辑]
检查类型是否可以从右值引用构造
(类模板) [编辑]
指定类型的对象可以默认构造
(概念) [编辑]