定义于头文件
<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 起) |
1) 若 T
不是可引用类型(即可为 cv 限定的 void 或带 cv-qualifier-seq 或 ref-qualifier 的函数类型),则提供等于 false 的成员常量 value
。否则,提供等于 std::is_constructible<T, const T&>::value 的成员常量 value
。
2) 同 (1) ,但使用 std::is_trivially_constructible<T, const T&> 。
3) 同 (1) ,但使用 std::is_nothrow_constructible<T, const T&> 。
T
应是完整类型、(可为 cv 限定的) void ,或未知边界数组。否则行为未定义。
目录 |
template< class T >
inline constexpr bool is_copy_constructible_v = is_copy_constructible<T>::value; |
(C++17 起) | |
template< class T >
inline constexpr bool is_trivially_copy_constructible_v = is_trivially_copy_constructible<T>::value; |
(C++17 起) | |
template< class T >
inline constexpr bool is_nothrow_copy_constructible_v = is_nothrow_copy_constructible<T>::value; |
(C++17 起) | |
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_copy_constructible : std::is_constructible<T, typename std::add_lvalue_reference< typename std::add_const<T>::type>::type>> {}; template<class T> struct is_trivially_copy_constructible : std::is_trivially_constructible<T, typename std::add_lvalue_reference< typename std::add_const<T>::type>::type>> {}; template<class T> struct is_nothrow_copy_constructible : std::is_nothrow_constructible<T, typename std::add_lvalue_reference< typename std::add_const<T>::type>::type>> {}; |
多数实现中, is_nothrow_copy_constructible
亦检查析构函数是否抛出,因为它等效于 noexcept(T(arg)) 。同样的情况应用于 is_trivially_copy_constructible
,它在这些实现中亦要求析构函数为平凡: GCC 漏洞 51452 、 LWG 问题 2116 。
#include <iostream> #include <type_traits> struct Ex1 { std::string str; // 成员拥有非平凡复制构造函数 }; struct Ex2 { int n; Ex2(const Ex2&) = default; // 平凡且不抛出 }; 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) |
检查类型是否能从右值引用构造 (类模板) |