std::is_constructible, std::is_trivially_constructible, std::is_nothrow_constructible
来自cppreference.com
                    
                                        
                    
                    
                                                            
                    |   定义于头文件  <type_traits>
  | 
||
|   template< class T, class... Args > struct is_constructible;  | 
(1) | (C++11 起) | 
|   template< class T, class... Args > struct is_trivially_constructible;  | 
(2) | (C++11 起) | 
|   template< class T, class... Args > struct is_nothrow_constructible;  | 
(3) | (C++11 起) | 
1) 若 
为此检查的目的,决不转译变量定义为函数声明,且不认为 std::declval 是 odr 使用。如同从与
T 是对象或引用类型,且变量定义 T obj(std::declval<Args>()...); 为良构,则提供等于 true 的成员常量 value 。所有其他情况下, value 为 false 。为此检查的目的,决不转译变量定义为函数声明,且不认为 std::declval 是 odr 使用。如同从与
T 和任何 Args 中类型无关的语境进行访问检查。仅考虑变量定义的立即语境的合法性。3) 同 (1) ,但变量定义为 
noexcept 。T 与参数包 Args 中的所有类型应均为完整类型、(可为 cv 限定的) void ,或未知边界数组。否则行为未定义。
若上述模板的实例化直接或间接地依赖于不完整类型,并且如果假如使该类型完整,实例化就会产生不同的结果,则行为未定义。
添加此页面上描述的任何模板的特化的程序行为未定义。
辅助变量模板
|   template< class T, class... Args > inline constexpr bool is_constructible_v = is_constructible<T, Args...>::value;  | 
(C++17 起) | |
|   template< class T, class... Args > inline constexpr bool is_trivially_constructible_v = is_trivially_constructible<T, Args...>::value;  | 
(C++17 起) | |
|   template< class T, class... Args > inline constexpr bool is_nothrow_constructible_v = is_nothrow_constructible<T, Args...>::value;  | 
(C++17 起) | |
继承自 std::integral_constant
成员常量
|    value [静态]  | 
   若 T 可从 Args... 构造则为 true ,否则为 false  (公开静态成员常量)  | 
成员函数
|    operator bool  | 
   转换对象为 bool ,返回 value  (公开成员函数)  | 
|    operator() (C++14)  | 
   返回 value  (公开成员函数)  | 
成员类型
| 类型 | 定义 | 
  value_type
 | 
  bool
 | 
  type
 | 
std::integral_constant<bool, value> | 
注解
多数实现中, is_nothrow_constructible 亦检查析构函数是否抛出,因为它等效于 noexcept(T(arg)) 。同样的情况应用于 is_trivially_constructible ,在这些实现中亦要求析构函数为平凡: GCC 漏洞 51452 、 LWG 问题 2116 。
示例
运行此代码
#include <iostream> #include <type_traits> class Foo { int v1; double v2; public: Foo(int n) : v1(n), v2() {} Foo(int n, double f) noexcept : v1(n), v2(f) {} }; int main() { std::cout << "Foo is ...\n" << std::boolalpha << "\tTrivially-constructible from const Foo&? " << std::is_trivially_constructible<Foo, const Foo&>::value << '\n' << "\tTrivially-constructible from int? " << std::is_trivially_constructible<Foo, int>::value << '\n' << "\tConstructible from int? " << std::is_constructible<Foo, int>::value << '\n' << "\tNothrow-constructible from int? " << std::is_nothrow_constructible<Foo, int>::value << '\n' << "\tNothrow-constructible from int and double? " << std::is_nothrow_constructible<Foo, int, double>::value << '\n'; }
输出:
Foo is ...
        Trivially-constructible from const Foo&? true
        Trivially-constructible from int? false
        Constructible from int? true
        Nothrow-constructible from int? false
        Nothrow-constructible from int and double? true参阅
|   检查类型是否有默认构造函数  (类模板)  | |
|    (C++11)(C++11)(C++11)  | 
  检查类型是否拥有复制构造函数  (类模板)  | 
|    (C++11)(C++11)(C++11)  | 
  检查类型是否能从右值引用构造  (类模板)  | 
|    (C++11)  | 
   检查指定的类型是否支持使用分配器的构造   (类模板)  |