C++ 具名要求-基本概念-指定该类型对象可以从右值赋值

指定该类型对象可以从右值赋值

指定该类型的实例可以从右值实参赋值。

要求

若满足下列条件,则类型 T 满足可移动赋值 (MoveAssignable)

给定

  • T 类型的可修改左值表达式 t
  • T 类型的右值表达式 rv

下列表达式必须合法并拥有其指定的效果

表达式返回类型返回值后条件
t = rvT&ttrv 不指代同一对象,则 t 的值等价于 rv 在赋值前的值。

rv 的新值未指定

注解

类型不必为满足此类型要求而实现移动赋值运算符:按值或按 const Type& 接收其参数的复制赋值运算符会绑定到右值实参。

可移动赋值 (MoveAssignable) 类实现了移动赋值运算符,则它亦可实现移动语义,以获得“rv 在赋值后的值未指定”这一事实的优势。

调用示例

#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;
    }
};

int main()
{
    std::cout << std::boolalpha;

    std::cout << "std::is_move_assignable<int>::value: "
              << std::is_move_assignable<int>::value << std::endl;
    std::cout << "std::is_trivially_move_assignable<int>::value: "
              << std::is_trivially_move_assignable<int>::value << std::endl;
    std::cout << "std::is_nothrow_move_assignable<int>::value: "
              << std::is_nothrow_move_assignable<int>::value << std::endl;
    std::cout << std::endl;

    std::cout << "std::is_move_assignable<A>::value: "
              << std::is_move_assignable<A>::value << std::endl;
    std::cout << "std::is_trivially_move_assignable<A>::value: "
              << std::is_trivially_move_assignable<A>::value << std::endl;
    std::cout << "std::is_nothrow_move_assignable<A>::value: "
              << std::is_nothrow_move_assignable<A>::value << std::endl;
    std::cout << std::endl;

    std::cout << "std::is_move_assignable<B>::value: "
              << std::is_move_assignable<B>::value << std::endl;
    std::cout << "std::is_trivially_move_assignable<B>::value: "
              << std::is_trivially_move_assignable<B>::value << std::endl;
    std::cout << "std::is_nothrow_move_assignable<B>::value: "
              << std::is_nothrow_move_assignable<B>::value << std::endl;
    std::cout << std::endl;

    std::cout << "std::is_move_assignable<C>::value: "
              << std::is_move_assignable<C>::value << std::endl;
    std::cout << "std::is_trivially_move_assignable<C>::value: "
              << std::is_trivially_move_assignable<C>::value << std::endl;
    std::cout << "std::is_nothrow_move_assignable<C>::value: "
              << std::is_nothrow_move_assignable<C>::value << std::endl;
    std::cout << std::endl;

    //t = rv T& t 若 t 与 rv 不指代同一对象,则 t 的值等价于 rv 在赋值前的值。rv 的新值未指定
    MyClass() = std::move(MyClass(101, 102));

    return 0;
}

输出

std::is_move_assignable<int>::value: true
std::is_trivially_move_assignable<int>::value: true
std::is_nothrow_move_assignable<int>::value: true

std::is_move_assignable<A>::value: true
std::is_trivially_move_assignable<A>::value: true
std::is_nothrow_move_assignable<A>::value: true

std::is_move_assignable<B>::value: true
std::is_trivially_move_assignable<B>::value: false
std::is_nothrow_move_assignable<B>::value: false

std::is_move_assignable<C>::value: true
std::is_trivially_move_assignable<C>::value: false
std::is_nothrow_move_assignable<C>::value: false

0x61fe80  MyClass 35 a:101 b:102
0x61fe88  MyClass 28 a:101 b:102
0x61fe88  operator= 62 a:101 b:102

  • 9
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值