【发布时间】:2012-07-23 04:03:44
【问题描述】:
考虑这段代码(renew 和 cleanse 的不同值):
struct T {
int mem;
T() { }
~T() { mem = 42; }
};
// identity functions,
// but breaks any connexion between input and output
int &cleanse_ref(int &r) {
int *volatile pv = &r; // could also use cin/cout here
return *pv;
}
void foo () {
T t;
int &ref = t.mem;
int &ref2 = cleanse ? cleanse_ref(ref) : ref;
t.~T();
if (renew)
new (&t) T;
assert(ref2 == 42);
exit(0);
}
assert 是否保证通过?
我了解不推荐这种风格。 像“这不是一个正确的做法”这样的意见在这里不感兴趣。
我想要一个显示标准引用的完整逻辑证明的答案。编译器作者的意见也可能很有趣。
编辑:现在将两个问题合二为一!见renew参数(带renew == 0,这是原问题)。
编辑 2:我想我的问题真的是:什么是成员对象?
编辑 3:现在使用另一个 cleanse 参数!
【问题讨论】:
-
反对票很快... :(
-
我不明白为什么。这是个有趣的问题。来自我的 +1。
-
如果您不喜欢这个问题,我建议您将“language-lawyer”添加到您忽略的标签列表中。
-
&p是什么?没有声明p。 -
标准不允许您提供证明。在对象生命周期定义方面存在缺陷。
标签: c++ destructor language-lawyer object-lifetime explicit-destructor-call