Namespaces
Variants

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

From cppreference.net
Metaprogramming library
Type traits
Type categories
(C++11)
(C++11) ( DR* )
Type properties
(C++11)
(C++11)
(C++14)
(C++11) (deprecated in C++26)
(C++11) ( until C++20* )
(C++11) (deprecated in C++20)
(C++11)
Type trait constants
Metafunctions
(C++17)
Supported operations
Relationships and property queries
Type modifications
Type transformations
(C++11) (deprecated in C++23)
(C++11) (deprecated in C++23)
(C++11)
(C++11) ( until C++20* ) (C++17)

Compile-time rational arithmetic
Compile-time integer sequences
ヘッダーで定義 <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 false
(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から継承

メンバ定数

value
[static]
true Tがコピー構築可能な場合は 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 bug 51452 LWG issue 2116

#include <string>
#include <type_traits>
struct S1
{
    std::string str; // メンバーが非自明なコピーコンストラクタを持つ
};
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; // 自明かつ非スロー
};
static_assert(std::is_trivially_copy_constructible_v<S2>);
static_assert(std::is_nothrow_copy_constructible_v<S2>);
struct S3
{
    S3(const S3&) = delete; // 明示的に削除
};
static_assert(!std::is_copy_constructible_v<S3>);
struct S4
{
    S4(S4&) {}; // constをバインドできないため、コピー構築可能ではない
};
static_assert(!std::is_copy_constructible_v<S4>);
int main() {}

欠陥報告

以下の動作変更の欠陥報告書は、以前に公開されたC++規格に対して遡及的に適用されました。

DR 適用対象 公開時の動作 正しい動作
LWG 2196 C++11 const T & が形成できない場合の動作が不明確であった この場合の生成値は false である

関連項目

特定の引数に対するコンストラクタが型に存在するかチェックする
(クラステンプレート)
型がデフォルトコンストラクタを持つかチェックする
(クラステンプレート)
型が右辺値参照から構築可能かチェックする
(クラステンプレート)
型のオブジェクトがコピー構築およびムーブ構築可能であることを指定する
(コンセプト)