此页面中列出的具名要求,是 C++ 标准的规范性文本中使用的具名要求,用于定义标准库的期待。
某些具名要求在 C++20 中正在以概念语言特性进行形式化。在那之前,确保以满足这些要求的模板实参实例化标准库模板是程序员的重担。若不这么做,则可能导致非常复杂的编译器诊断。
指定该类型对象可以从左值赋值
指定该类型的实例可从左值表达式复制赋值。
要求
若满足下列条件,则类型 T
满足可复制赋值 (CopyAssignable) :
- 类型
T
满足可移动赋值 (MoveAssignable) ,且
给定
T
类型的可修改左值表达式t
v
,为T
或const T
类型的左值表达式或const T
类型的右值表达式
下列表达式必须合法且拥有其指定的效果
表达式 | 返回类型 | 返回值 | 后条件 |
---|---|---|---|
t = v | T& |
t |
t 的值等价于 v 的值。
|
调用示例
#include <iostream>
#include <type_traits>
//编译器生成默认构造函数
struct A
{
};
struct B
{
std::string str; // 成员拥有非平凡默认构造函数
};
struct C
{
std::string str; // 成员拥有非平凡默认构造函数
C() throw (int) //构造函数抛异常
{
}
};
struct MyClass
{
int ma;
int mb;
MyClass(): ma(101), mb(102)
{
std::cout << this << " " << __FUNCTION__ << " " << __LINE__
<< " a:" << ma << " b:" << mb
<< std::endl;
}
MyClass(int a, int b): ma(a), mb(b)
{
std::cout << this << " " << __FUNCTION__ << " " << __LINE__
<< " a:" << ma << " b:" << mb
<< std::endl;
}
MyClass(const MyClass &obj)
{
this->ma = obj.ma;
this->mb = obj.mb;
std::cout << this << " " << __FUNCTION__ << " " << __LINE__
<< " a:" << ma << " b:" << mb
<< std::endl;
}
MyClass(MyClass &&obj)
{
this->ma = obj.ma;
this->mb = obj.mb;
std::cout << this << " " << __FUNCTION__ << " " << __LINE__
<< " a:" << ma << " b:" << mb
<< std::endl;
}
MyClass & operator =(MyClass &&obj)
{
this->ma = obj.ma;
this->mb = obj.mb;
std::cout << this << " " << __FUNCTION__ << " " << __LINE__
<< " a:" << ma << " b:" << mb
<< std::endl;
return *this;
}
MyClass & operator =(const MyClass &obj)
{
this->ma = obj.ma;
this->mb = obj.mb;
std::cout << this << " " << __FUNCTION__ << " " << __LINE__
<< " a:" << ma << " b:" << mb
<< std::endl;
return *this;
}
};
int main()
{
std::cout << std::boolalpha;
std::cout << "std::is_copy_assignable<int>::value: "
<< std::is_copy_assignable<int>::value << std::endl;
std::cout << "std::is_trivially_copy_assignable<int>::value: "
<< std::is_trivially_copy_assignable<int>::value << std::endl;
std::cout << "std::is_nothrow_copy_assignable<int>::value: "
<< std::is_nothrow_copy_assignable<int>::value << std::endl;
std::cout << std::endl;
std::cout << "std::is_copy_assignable<A>::value: "
<< std::is_copy_assignable<A>::value << std::endl;
std::cout << "std::is_trivially_copy_assignable<A>::value: "
<< std::is_trivially_copy_assignable<A>::value << std::endl;
std::cout << "std::is_nothrow_copy_assignable<A>::value: "
<< std::is_nothrow_copy_assignable<A>::value << std::endl;
std::cout << std::endl;
std::cout << "std::is_copy_assignable<B>::value: "
<< std::is_copy_assignable<B>::value << std::endl;
std::cout << "std::is_trivially_copy_assignable<B>::value: "
<< std::is_trivially_copy_assignable<B>::value << std::endl;
std::cout << "std::is_nothrow_copy_assignable<B>::value: "
<< std::is_nothrow_copy_assignable<B>::value << std::endl;
std::cout << std::endl;
std::cout << "std::is_copy_assignable<C>::value: "
<< std::is_copy_assignable<C>::value << std::endl;
std::cout << "std::is_trivially_copy_assignable<C>::value: "
<< std::is_trivially_copy_assignable<C>::value << std::endl;
std::cout << "std::is_nothrow_copy_assignable<C>::value: "
<< std::is_nothrow_copy_assignable<C>::value << std::endl;
std::cout << std::endl;
MyClass myClass1 = {101, 102};
//t = rv T& t 若 t 与 rv 不指代同一对象,则 t 的值等价于 rv 在赋值前的值。rv 的新值未指定
MyClass() = myClass1;
return 0;
}
输出
std::is_copy_assignable<int>::value: true
std::is_trivially_copy_assignable<int>::value: true
std::is_nothrow_copy_assignable<int>::value: true
std::is_copy_assignable<A>::value: true
std::is_trivially_copy_assignable<A>::value: true
std::is_nothrow_copy_assignable<A>::value: true
std::is_copy_assignable<B>::value: true
std::is_trivially_copy_assignable<B>::value: false
std::is_nothrow_copy_assignable<B>::value: false
std::is_copy_assignable<C>::value: true
std::is_trivially_copy_assignable<C>::value: false
std::is_nothrow_copy_assignable<C>::value: false
0x61fe80 MyClass 35 a:101 b:102
0x61fe88 MyClass 28 a:101 b:102
0x61fe88 operator= 72 a:101 b:102