名稱空間
變體
操作

std::is_copy_constructible, std::is_trivially_copy_constructible, std::is_nothrow_copy_constructible

來自 cppreference.com
< cpp‎ | 型別
 
 
超程式設計庫
型別特性
型別類別
(C++11)
(C++11)(DR*)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11) 
(C++11)
(C++11)
型別屬性
(C++11)
(C++11)
(C++14)
(C++11)(C++26 中已棄用)
(C++11)(直到 C++20*)
(C++11)(C++20 中已棄用)
(C++11)
型別特性常量
元函式
(C++17)
支援的操作
關係與屬性查詢
型別修改
(C++11)(C++11)(C++11)
型別轉換
(C++11)(C++23 中已棄用)
(C++11)(C++23 中已棄用)
(C++11)
(C++11)(直到 C++20*)(C++17)

(C++11)
(C++17)
編譯時有理數算術
編譯時整數序列
 
定義於標頭檔案 <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 =

    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 起)

繼承自 std::integral_constant

成員常量

[靜態]
trueT 為可複製構造的,否則為 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 51452LWG 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

[編輯] 參見

檢查型別是否具有針對特定引數的建構函式
(類模板) [編輯]
檢查型別是否具有預設建構函式
(類模板) [編輯]
檢查型別是否可以從右值引用構造
(類模板) [編輯]
指定型別的物件可以被複制構造和移動構造
(概念) [編輯]