std::is_copy_constructible, std::is_trivially_copy_constructible, std::is_nothrow_copy_constructible
来自cppreference.com
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
在头文件 <type_traits> 中定义
|
||
template< class T > struct is_copy_constructible; |
(1) | (C++11 起) |
template< class T > struct is_trivially_copy_constructible; |
(2) | (C++11 起) |
template< class T > struct is_nothrow_copy_constructible; |
(3) | (C++11 起) |
检查一个类型是否是
2) CopyConstructible
,即具有可访问的显式或隐式的拷贝构造函数。如果符合要求,一个成员value
等于true,否则value
是false.原文:
Checks whether a type is
CopyConstructible
, i.e. has an accessible explicit or implicit copy constructor. If the requirement is met, a member constant value
equal true is provided, otherwise value
is false.同(1),但拷贝构造函数的表达不调用任何操作,是不平凡的.
3) 原文:
Same as (1), but the copy constructor expression does not call any operation that is not trivial.
同(1),但拷贝构造函数的表达noexcept.
原文:
Same as (1), but the copy constructor expression is noexcept.
目录 |
Inherited from std::integral_constant
Member constants
value [静态的]</div></div>
|
true如果 T is copy-constructible ,false其他方式 原文: true if T is copy-constructible , 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>
[编辑] 可能的实现
template<class T> struct is_copy_constructible : std::is_constructible<T, const typename std::add_lvalue_reference<T>::type> {}; template<class T> struct is_trivially_copy_constructible : std::is_trivially_constructible<T, const typename std::add_lvalue_reference<T>::type> {}; template<class T> struct is_nothrow_copy_constructible : std::is_nothrow_constructible<T, const typename std::add_lvalue_reference<T>::type> {}; |
[编辑] 示例
#include <iostream> #include <type_traits> struct Ex1 { std::string str; // member has a non-trivial copy ctor }; struct Ex2 { int n; Ex2(const Ex2&) = default; // trivial and non-throwing }; int main() { std::cout << std::boolalpha << "Ex1 is copy-constructible? " << std::is_copy_constructible<Ex1>::value << '\n' << "Ex1 is trivially copy-constructible? " << std::is_trivially_copy_constructible<Ex1>::value << '\n' << "Ex2 is trivially copy-constructible? " << std::is_trivially_copy_constructible<Ex2>::value << '\n' << "Ex2 is nothrow copy-constructible? " << std::is_nothrow_copy_constructible<Ex2>::value << '\n'; }
输出:
Ex1 is copy-constructible? true Ex1 is trivially copy-constructible? false Ex2 is trivially copy-constructible? true Ex2 is nothrow copy-constructible? true
[编辑] 另请参阅
(C++11) (C++11) (C++11) |
检查对象是否实现了特定参数的构造函数 (类模板) |
检查类型是否使用默认构造函数 (类模板) | |
(C++11) (C++11) (C++11) |
检查对象是否声明了移动构造函数 (类模板) |