简体   繁体   English

在C ++标准中临时绑定到成员生命周期语句的重点是什么?

[英]What's the point of temporary bound to a member lifetime statement in C++ Standard?

In this question user Happy Mittal quotes section 12.2.5 of C++03 Standard: A temporary bound to a reference member in a constructor's ctor-initializer (12.6.2) persists until the constructor exits . 这个问题中,用户Happy Mittal引用了C ++ 03标准的第12.2.5节: 构造函数的ctor-initializer(12.6.2)中的引用成员的临时绑定一直存在,直到构造函数退出

How can that be useful anyway? 这怎么可能有用呢? I mean once the constructor exits the temporary gets destroyed, but the reference remains bound - now to an already destroyed object. 我的意思是一旦构造函数退出临时被破坏,但引用仍然绑定 - 现在已经被破坏的对象。

What's the point of so carefully specifying the temporary lifetime if there's still a dangling reference for the whole lifetime of the outer object? 如果在外部对象的整个生命周期中仍然存在悬空引用,那么如何仔细指定临时生命周期又有什么意义呢? In which scenario can this behavior be useful? 在哪种情况下这种行为有用吗?

It is not useful to have a reference member bound to a dead object, but it is useful to be clear that "normal" temporary lifetime extension when bound to a reference doesn't apply in this case. 将引用成员绑定到死对象是没有用的,但是明确指出绑定到引用时的“正常”临时生命周期扩展在这种情况下不适用是有用的。

It also specifies temporary lifetime extension that applies specially in the ctor initializer: it's extended to the end of the ctor rather than dying before the ctor body executes. 它还指定了临时生命周期扩展,它特别适用于ctor初始化器:它延伸到ctor的末尾,而不是在ctor体执行之前死亡。 This would not be useful except in "clever" classes whose whole point is executing the ctor, and this type of (ab)use is rightly avoided. 除了“巧妙”类之外,这一点没有用,它的全部意义在于执行ctor,并且正确地避免了这种类型的(ab)使用。

I know of no real world examples of the latter, but it strikes me akin to having destructors nothrow by default broke classes that were "clever" in their lifetime and how they were used. 我知道后者没有现实世界的例子,但它让我感觉类似于让析构函数默认情况下破坏了在他们的生命中“聪明”以及如何使用它们的类。 This did have real world uses and came up in discussions about how to handle the default semantics of dtors in C++0x. 这确实有现实的用途和想出了在如何处理dtors的默认语义的C ++ 0x的讨论。

In D language, construction process can be written freely in some degree. 在D语言中,构建过程可以在一定程度上自由编写。 However in C++, construction/initialization order is strictly stipulated. 但是在C ++中,严格规定了构造/初始化顺序。 So if class initialization requires some expensive computation, a code like the following sometimes may be valid as a reluctant workaround. 因此,如果类初始化需要一些昂贵的计算,那么像下面这样的代码有时可能是一个不情愿的解决方法。

struct S {
  Args const &r;
  A a;
  B b;
  S( args.... )
    : r( expensive_func( args.... ) ), a( r.for_a ), b( r.for_b ) {}
};

It's useful for compiler writers. 它对编译器编写者很有用。 They already have logic in place to destroy bound temporaries at the end of a scope, and the exit of the constructor is one such point. 他们已经有了逻辑来破坏作用域末尾的绑定临时对象,构造函数的退出就是这样一个点。 With this rule compilers can reuse that point to destroy such temporaries as well. 有了这个规则,编译器就可以重用这一点来摧毁这些临时工。

Note that the standard really should decide on some lifetime, and the only other reasonable point would be after the ctor initializer list but before the ctor body. 请注意,标准确实应该决定一些生命周期,唯一的另一个合理点是在ctor初始化列表之后但在ctor体之前。 That's not a point where temporaries would be destroyed otherwise, and it could interfere with function-scope try {} catch() blocks (which do include the ctor initializer list) 这不是临时销毁的点,否则会干扰函数范围try {} catch()块(包括ctor初始化列表)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM