非平凡可复制类型的值表示 [英] Value representation of non-trivially copyable types

查看:166
本文介绍了非平凡可复制类型的值表示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对标准的以下段落( ISO / IEC 14882:2011(E)的第3.9 / 4节)很感兴趣:

I'm intrigued by the following paragraph from the standard (§3.9/4 of ISO/IEC 14882:2011(E)):


T 类型的对象的对象表示 N 无符号字符对象被类型为 T 的对象占用,其中 N 等于 sizeof(T)。对象的值表示形式是持有 T 类型值的位的集合。对于平凡可复制的类型,值表示形式是对象表示形式中的一组位,用于确定 value ,该值是实现定义的一组值中的一个离散元素。 42

The object representation of an object of type T is the sequence of N unsigned char objects taken up by the object of type T, where N equals sizeof(T). The value representation of an object is the set of bits that hold the value of type T. For trivially copyable types, the value representation is a set of bits in the object representation that determines a value, which is one discrete element of an implementation-defined set of values.42

我了解对象表示形式值表示形式与允许某些对象表示不参与对象的值(例如,填充)。不过,我不太了解普通可复制类型。非平凡可复制类型没有值吗?非平凡可复制类型的值表示形式的一部分可以存在于其对象表示形式之外吗?

I understand that the object representation and value representation are distinct to allow some of the object representation to not take part in the value of the object (for example, padding). I don't quite get the point about trivially copyable types though. Do non-trivially copyable types not have values? Can part of the value representation of a non-trivially copyable type exist outside its object representation?

注42解释:


目的是C ++的内存模型与ISO / IEC 9899编程语言C兼容。

The intent is that the memory model of C++ is compatible with that of ISO/IEC 9899 Programming Language C.

我仍然不明白为什么前面的语句只适用于琐碎的可复制类型。

I still don't understand why the previous statement is specifically for trivially copyable types only though. What is the significance of this?

推荐答案

标准示例是管理资源的类:

The standard example is a class that manages a resource:

struct Foo
{
    Bar * p;

    Foo() : p(new Bar) { }
    ~Foo() { delete p; }

    // copy, assign
};

Foo 类型的对象具有值,但是该值不能通过复制对象表示形式来复制(在这种情况下,它只是 p 的值)。复制 Foo 类型的对象需要复制该类的 se­ man­ tics ,该类说一个对象拥有 >尖兵。因此,合适的副本需要一个合适的,用户定义的副本构造函数:

An object of type Foo has a value, but that value is not copyable by copying the object representation (which is just the value of p in this case). Copying an object of type Foo requires copying the se­man­tics of the class, which say "an object owns the pointee". A suitable copy thus requires an appropriate, user-defined copy constructor:

Foo::Foo(Foo const & rhs) : p(new Bar(*rhs.p)) { }

现在对象的对象表示为类型 Foo 与此类对象的副本的对象表示形式不同,尽管它们的值相同。

Now the object representation of an object of type Foo is different from the object representation of a copy of such an object, although they have the same value.

相反,只要对象表示重合, int 的值就与另一个 int 的值相同。 (由于填充,这是一个充分的条件,但不是必需的。)

By contrast, the value of an int is the same as that of another int as soon as the object representations coincide. (This is a sufficient, though not necessary, condition, due to padding.)

这篇关于非平凡可复制类型的值表示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆