std::is_copy_constructible, std::is_trivially_copy_constructible, std::is_nothrow_copy_constructible
來自 cppreference.com
定義於標頭檔案 <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 起) |
型別特徵 | 成員常量 value 的值 | |
---|---|---|
T 是一個可引用型別 |
T 不是可引用型別 | |
(1) | std::is_constructible<T, const T&>::value | 假 |
(2) | std::is_trivially_constructible<T, const T&>::value | |
(3) | std::is_nothrow_constructible<T, const T&>::value |
如果 T
不是完整型別,(可能 cv 限定的) void,或未知邊界陣列,則行為未定義。
如果上述模板的例項化直接或間接依賴於不完整型別,並且該例項化在該型別假設完成時可能產生不同的結果,則行為未定義。
如果程式為此頁上描述的任何模板新增特化,則行為未定義。
目錄 |
[編輯] 輔助變數模板
template< class T > inline constexpr bool is_copy_constructible_v = |
(C++17 起) | |
template< class T > inline constexpr bool is_trivially_copy_constructible_v = |
(C++17 起) | |
template< class T > inline constexpr bool is_nothrow_copy_constructible_v = |
(C++17 起) | |
繼承自 std::integral_constant
成員常量
值 [靜態] |
true 若 T 為可複製構造的,否則為 false(public static 成員常量) |
成員函式
operator bool |
將物件轉換為 bool,返回 value (公開成員函式) |
operator() (C++14) |
返回 value (公開成員函式) |
成員型別
型別 | 定義 |
value_type
|
bool |
型別
|
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 bug 51452,LWG issue 2116。
[編輯] 示例
執行此程式碼
#include <string> #include <type_traits> struct S1 { std::string str; // member has a non-trivial copy constructor }; static_assert(std::is_copy_constructible_v<S1>); static_assert(!std::is_trivially_copy_constructible_v<S1>); struct S2 { int n; S2(const S2&) = default; // trivial and non-throwing }; static_assert(std::is_trivially_copy_constructible_v<S2>); static_assert(std::is_nothrow_copy_constructible_v<S2>); struct S3 { S3(const S3&) = delete; // explicitly deleted }; static_assert(!std::is_copy_constructible_v<S3>); struct S4 { S4(S4&) {}; // cannot bind const, hence not a copy-constructible }; static_assert(!std::is_copy_constructible_v<S4>); int main() {}
[編輯] 缺陷報告
下列更改行為的缺陷報告追溯地應用於以前出版的 C++ 標準。
缺陷報告 | 應用於 | 釋出時的行為 | 正確的行為 |
---|---|---|---|
LWG 2196 | C++11 | 若 const T& 無法形成,則行為不明確 | 在這種情況下,生成的值為 false |
[編輯] 參見
(C++11)(C++11)(C++11) |
檢查型別是否具有針對特定引數的建構函式 (類模板) |
檢查型別是否具有預設建構函式 (類模板) | |
(C++11)(C++11)(C++11) |
檢查型別是否可以從右值引用構造 (類模板) |
(C++20) |
指定型別的物件可以被複制構造和移動構造 (概念) |