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

本文详细讨论了C++中对象的可复制构造(CopyConstructible)和移动构造(MoveConstructible)的概念,以及它们在不同类型结构体中的实现和应用,特别关注了左值和右值表达式、默认构造函数以及是否支持无异常复制的情况。
摘要由CSDN通过智能技术生成

指定该类型对象可以从左值构造

指定该类型的实例可以从左值表达式进行复制构造。

要求

以下情况下,类型 T 满足可复制构造 (CopyConstructible)

  • 类型 T 满足可移动构造 (MoveConstructible) ,且

给定

  • v,为 Tconst T 类型的左值表达式或 const T 类型的右值表达式
  • 任意标识符 u

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

表达式后条件
T u = v;u 的值等价于 v 的值。不更改 v 的值。
T(v)T(v) 的值等价于 v 的值。不更改 v 的值。

表达式 v.~T() 亦必须合法,且对于左值 v,表达式 &v 必须具有 T* 或 const T* 类型,且必须求值为 v 的地址。

(C++11 前)

注解

C++11 前,重载了 operator& 的类并非是可复制构造 (CopyConstructible) ,从而不可用于标准库容器。C++11 开始,标准库在凡是需要对象地址时都使用 std::addressof。

调用示例

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

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

    std::cout << "std::is_copy_constructible<int>::value: "
              << std::is_copy_constructible<int>::value << std::endl;
    std::cout << "std::is_trivially_copy_constructible<int>::value: "
              << std::is_trivially_copy_constructible<int>::value << std::endl;
    std::cout << "std::is_nothrow_copy_constructible<int>::value: "
              << std::is_nothrow_copy_constructible<int>::value << std::endl;
    std::cout << std::endl;

    std::cout << "std::is_copy_constructible<A>::value: "
              << std::is_copy_constructible<A>::value << std::endl;
    std::cout << "std::is_trivially_copy_constructible<A>::value: "
              << std::is_trivially_copy_constructible<A>::value << std::endl;
    std::cout << "std::is_nothrow_copy_constructible<A>::value: "
              << std::is_nothrow_copy_constructible<A>::value << std::endl;
    std::cout << std::endl;

    std::cout << "std::is_copy_constructible<B>::value: "
              << std::is_copy_constructible<B>::value << std::endl;
    std::cout << "std::is_trivially_copy_constructible<B>::value: "
              << std::is_trivially_copy_constructible<B>::value << std::endl;
    std::cout << "std::is_nothrow_copy_constructible<B>::value: "
              << std::is_nothrow_copy_constructible<B>::value << std::endl;
    std::cout << std::endl;

    std::cout << "std::is_copy_constructible<C>::value: "
              << std::is_copy_constructible<C>::value << std::endl;
    std::cout << "std::is_trivially_copy_constructible<C>::value: "
              << std::is_trivially_copy_constructible<C>::value << std::endl;
    std::cout << "std::is_nothrow_copy_constructible<C>::value: "
              << std::is_nothrow_copy_constructible<C>::value << std::endl;
    std::cout << std::endl;

    MyClass myClass1 = {101, 102};

    //T u = v; u 的值等价于 v 的值。不更改 v 的值。
    MyClass myClass2 = myClass1;


    //T(v) T(v) 的值等价于 v 的值。不更改 v 的值。
    MyClass myClass3(myClass2);

    return 0;
}

输出

std::is_copy_constructible<int>::value: true
std::is_trivially_copy_constructible<int>::value: true
std::is_nothrow_copy_constructible<int>::value: true

std::is_copy_constructible<A>::value: true
std::is_trivially_copy_constructible<A>::value: true
std::is_nothrow_copy_constructible<A>::value: true

std::is_copy_constructible<B>::value: true
std::is_trivially_copy_constructible<B>::value: false
std::is_nothrow_copy_constructible<B>::value: false

std::is_copy_constructible<C>::value: true
std::is_trivially_copy_constructible<C>::value: false
std::is_nothrow_copy_constructible<C>::value: false

0x61fe88  MyClass a:101 b:102
0x61fe80  MyClass a:101 b:102
0x61fe78  MyClass a:101 b:102

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值