std::is_constructible, std::is_trivially_constructible, std::is_nothrow_constructible
来自cppreference.com
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
在头文件 <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 起) |
如果表达式T obj(arg1, arg2, ... argN);是良好的,右值引用
2) Args...
作为参数,提供成员value
等于true。对于任何其他类型,value
是false.原文:
If the expression T obj(arg1, arg2, ... argN); is well-formed, given rvalue references to
Args...
as arguments, provides the member constant value
equal true. For any other type, value
is false.1),但构造表达不调用任何操作,是不平凡的.
3) 原文:
same as 1), but the constructor expression does not call any operation that is not trivial.
为1相同),但构造表达式noexcept.
原文:
same as 1), but the constructor expression is noexcept.
目录 |
Inherited from std::integral_constant
Member constants
value [静态的]</div></div>
|
true如果 T is constructible from Args... ,false其他方式 原文: true if T is constructible from Args... , false otherwise (公共静态成员常量) |
Member functions
operator bool |
转换的对象bool,返回 value 原文: converts the object to bool, returns value (公共成员函数) |
Member types
类型
|
Definition |
value_type
|
bool
|
type
|
std::integral_constant<bool, value> |
</div>
[编辑] 示例
#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) |
检查如果指定的类型支持使用分配器建设的 原文: checks if the specified type supports uses-allocator construction (类模板) |