Namespaces
Variants

std:: is_copy_assignable, std:: is_trivially_copy_assignable, std:: is_nothrow_copy_assignable

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
is_copy_assignable is_trivially_copy_assignable is_nothrow_copy_assignable
(C++11) (C++11) (C++11)

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_assignable ;
(1) (C++11以降)
template < class T >
struct is_trivially_copy_assignable ;
(2) (C++11以降)
template < class T >
struct is_nothrow_copy_assignable ;
(3) (C++11以降)
型特性 メンバ定数 value の値
T 参照可能な型 の場合 T が参照可能な型でない場合
(1) std:: is_assignable < T & , const T & > :: value false
(2) std:: is_trivially_assignable < T & , const T & > :: value
(3) std:: is_nothrow_assignable < T & , const T & > :: value

T が完全型でない場合、(おそらくcv修飾された) void または不明な境界の配列である場合、動作は未定義です。

上記のテンプレートのインスタンス化が、直接的または間接的に不完全型に依存しており、その型が仮に完全化された場合に異なる結果をもたらす可能性がある場合、動作は未定義です。

プログラムがこのページで説明されているテンプレートのいずれかに対する特殊化を追加する場合、動作は未定義です。

目次

ヘルパー変数テンプレート

template < class T >

inline constexpr bool is_copy_assignable_v =

is_copy_assignable < T > :: value ;
(C++17以降)
template < class T >

inline constexpr bool is_trivially_copy_assignable_v =

is_trivially_copy_assignable < T > :: value ;
(C++17以降)
template < class T >

inline constexpr bool is_nothrow_copy_assignable_v =

is_nothrow_copy_assignable < 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_assignable
    : std::is_assignable<typename std::add_lvalue_reference<T>::type,
                         typename std::add_lvalue_reference<const T>::type> {};
template<class T>
struct is_trivially_copy_assignable
    : std::is_trivially_assignable<typename std::add_lvalue_reference<T>::type,
                                   typename std::add_lvalue_reference<const T>::type> {};
template<class T>
struct is_nothrow_copy_assignable
    : std::is_nothrow_assignable<typename std::add_lvalue_reference<T>::type,
                                 typename std::add_lvalue_reference<const T>::type> {};

注記

トレイト std::is_copy_assignable は、 CopyAssignable よりも厳格さが低い。なぜなら、代入演算の結果の型( CopyAssignable 型では T 型の左辺値でなければならない)をチェックせず、引数式が変更されないという意味論的な要件もチェックしないためである。また、すべての CopyAssignable 型に要求される MoveAssignable を満たすかどうかもチェックしない。

#include <iostream>
#include <type_traits>
#include <utility>
struct Foo { int n; };
int main()
{
    std::cout << std::boolalpha
              << "Foo is trivially copy-assignable? "
              << std::is_trivially_copy_assignable<Foo>::value << '\n'
              << "int[2] is copy-assignable? "
              << std::is_copy_assignable<int[2]>::value << '\n'
              << "int is nothrow copy-assignable? "
              << std::is_nothrow_copy_assignable<int>::value << '\n';
}

出力:

Foo is trivially copy-assignable? true
int[2] is copy-assignable? false
int is nothrow copy-assignable? true

欠陥報告

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

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

関連項目

型が特定の引数に対する代入演算子を持つかどうかをチェックする
(クラステンプレート)
型がムーブ代入演算子を持つかどうかをチェックする
(クラステンプレート)