std::is_default_constructible, std::is_trivially_default_constructible, std::is_nothrow_default_constructible
来自cppreference.com
                    
                                        
                    
                    
                                                            
                    |   在标头  <type_traits> 定义
  | 
||
|   template< class T > struct is_default_constructible;  | 
(1) | (C++11 起) | 
|   template< class T > struct is_trivially_default_constructible;  | 
(2) | (C++11 起) | 
|   template< class T > struct is_nothrow_default_constructible;  | 
(3) | (C++11 起) | 
T  应该是完整类型、(可有 cv 限定的)void,或未知边界数组。否则行为未定义。
如果上述模板的实例化直接或间接地依赖于不完整类型,并且假设在该类型完整的情况下实例化就会产生不同的结果,那么行为未定义。
添加此页面上描述的任何模板的特化的程序行为未定义。
辅助变量模板
|   template< class T > inline constexpr bool is_default_constructible_v = is_default_constructible<T>::value;  | 
(C++17 起) | |
|   template< class T > inline constexpr bool is_trivially_default_constructible_v = is_trivially_default_constructible<T>::value;  | 
(C++17 起) | |
|   template< class T > inline constexpr bool is_nothrow_default_constructible_v = is_nothrow_default_constructible<T>::value;  | 
(C++17 起) | |
继承自 std::integral_constant
成员常量
|    value [静态]  | 
   如果  T 可默认构造那么是 true,否则是 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> {};  | 
注解
多数实现中,is_nothrow_default_constructible 也检查析构函数是否抛出,因为它等效于 noexcept(T()) 。同样的情况应用于 is_trivially_default_constructible,它在这些实现中也要求析构函数为平凡: GCC 漏洞 51452 、 LWG 问题 2116。 
示例
运行此代码
#include <iostream> #include <type_traits> struct Ex1 { std::string str; // 成员拥有非平凡默认构造函数 }; struct Ex2 { int n; Ex2() = default; // 平凡且不抛出 }; int main() { std::cout << std::boolalpha << "Ex1 is default-constructible? " << std::is_default_constructible<Ex1>::value << '\n' << "Ex1 is trivially default-constructible? " << std::is_trivially_default_constructible<Ex1>::value << '\n' << "Ex2 is trivially default-constructible? " << std::is_trivially_default_constructible<Ex2>::value << '\n' << "Ex2 is nothrow default-constructible? " << std::is_nothrow_default_constructible<Ex2>::value << '\n'; }
输出:
Ex1 is default-constructible? true Ex1 is trivially default-constructible? false Ex2 is trivially default-constructible? true Ex2 is nothrow default-constructible? true
参阅
|    (C++11)(C++11)(C++11)  | 
   检查类型是否带有针对特定实参的构造函数   (类模板)  | 
|    (C++11)(C++11)(C++11)  | 
  检查类型是否拥有复制构造函数  (类模板)  | 
|    (C++11)(C++11)(C++11)  | 
  检查类型是否能从右值引用构造  (类模板)  | 
|    (C++20)  | 
  指定能默认构造一个类型的对象  (概念)  |