命名空间
变体
操作

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

来自 cppreference.cn
< cpp‎ | types
 
 
元编程库
类型特征
类型类别
(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)
支持的操作
is_copy_assignableis_trivially_copy_assignableis_nothrow_copy_assignable
(C++11)(C++11)(C++11)

关系和属性查询
类型修改
(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_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
[静态]
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_assignableCopyAssignable 要求更宽松,因为它不检查赋值结果的类型(对于 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++ 标准。

DR 应用于 已发布行为 正确行为
LWG 2196 C++11 如果无法形成 const T&,则行为不明确 在这种情况下,产生的值为 false

[编辑] 参见

检查类型是否具有用于特定参数的赋值运算符
(类模板) [编辑]
检查类型是否具有移动赋值运算符
(类模板) [编辑]