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]
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_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 类型的左值),也不检查参数表达式保持不变的语义要求。同时它不验证 T 是否满足 MoveAssignable 要求,而这一要求对所有 CopyAssignable 类型都是必需的。

示例

#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++ 标准。

缺陷报告 适用范围 发布时行为 正确行为
LWG 2196 C++11 当无法形成 const T & 时行为不明确 此种情况下产生的值为 false

参见

检查类型是否拥有特定参数的赋值运算符
(类模板)
检查类型是否拥有移动赋值运算符
(类模板)