成员子对象继承存储持续时间有没有任何不直观的副作用? [英] Are there any unintuitive side-effects of member subobjects inheriting storage duration?

查看:204
本文介绍了成员子对象继承存储持续时间有没有任何不直观的副作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以前不知道,但结果证明


[C ++ 11:3.7.5]:成员子对象,基类子对象和数组元素的存储持续时间是它们的完整对象(1.8)的存储持续时间。


x-> a 在下面的示例中有动态存储持续时间。



我想知道是否有任何其他定义的语义,引用对象 * x 和<$之间不同行为的成员 a 的存储持续时间c $ c> y ?一个例子是管理对象生命周期的规则。

  struct T 
{
int a;
};

int main()
{
std :: unique_ptr< T> x(new T);
T y;
}

如果 T

简而言之,我的蜥蜴大脑希望任何声明看起来像 int a; 具有自动(或静态)存储持续时间,我不知道任何标准措辞是否也意外地预期了这一点。






更新:



以下是一个示例:

$ b $或者,实现可以具有严格的指针安全性,在这种情况下,指针值(例如,除非引用的完整对象是动态存储持续时间,否则不是安全派生的指针值是无效的指针值 [..]


在表面上,我不会指望我的 x-> a 和我的 ya ,但很明显,有些区域与对象生命周期没有明显的关联。



关注lambda捕获规则,它在多个地方显式地声明具有自动存储持续时间,例如:


[C ++ 11:5.1.2 / 11]:如果一个表达式有一个相关联的捕获默认 > odr-uses(3.2)具有自动存储持续时间的变量

[C ++ 11:5.1.2 / 18]:每次出现 decltype ))其中 x 是可能用括号括起来的 id-expression ,名称 / strong>被视为 x 被转换为对闭包类型的相应数据成员的访问,如果 x 是指定实体的odr使用。


和其他。

解决方案

否。这个存储持续时间的继承是使子对象工作的原因。做其他事情根本不可能。否则,你不能设计任何可以静态和动态分配的类型。



简单地说,任何违反这个规则都会破坏一切。


I didn't know this before, but it turns out that:

[C++11: 3.7.5]: The storage duration of member subobjects, base class subobjects and array elements is that of their complete object (1.8).

That means that x->a in the example below has dynamic storage duration.

I'm wondering whether there are any elsewhere-defined semantics that make reference to storage duration that imbue member a with different behaviour between object *x and y? An example would be the rules governing object lifetime.

struct T
{
   int a;
};

int main()
{
   std::unique_ptr<T> x(new T);
   T y;
}

And how about if T were non-POD (and other kinds of UDTs)?

In short, my lizard brain expects any declaration looking like int a; to have automatic (or static) storage duration, and I wonder whether any standard wording accidentally expects this too.


Update:

Here's an example:

[C++11: 3.7.4.3/4]: [..] Alternatively, an implementation may have strict pointer safety, in which case a pointer value that is not a safely-derived pointer value is an invalid pointer value unless the referenced complete object is of dynamic storage duration [..]

On the surface of it, I wouldn't expect the semantics to differ between my x->a and my y.a, but it's clear that there are areas, that are not obviously related to object lifetime, where they do.

I'm also concerned about lambda capture rules, which explicitly state "with automatic storage duration" in a number of places, e.g.:

[C++11: 5.1.2/11]: If a lambda-expression has an associated capture-default and its compound-statement odr-uses (3.2) this or a variable with automatic storage duration [..]

[C++11: 5.1.2/18]: Every occurrence of decltype((x)) where x is a possibly parenthesized id-expression that names an entity of automatic storage duration is treated as if x were transformed into an access to a corresponding data member of the closure type that would have been declared if x were an odr-use of the denoted entity.

and others.

解决方案

No. This storage duration inheritance is what makes subobjects work. Doing anything else would simply be quite impossible. Else, you could not design any type that could be allocated both statically and dynamically.

Simply put, any violation of this rule would simply break everything.

这篇关于成员子对象继承存储持续时间有没有任何不直观的副作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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