wal*_*nut 7 c++ language-lawyer
C ++标准未指定临时对象的存储期限。这是CWG第1634期的主题。
根据链接的网站,此问题的状态为“草稿”,表示:
起草:尽管尚无确切的措辞,但工作组已达成非正式共识,并在暂定决议中对此进行了粗略的描述。
但是,没有明确提及针对该特定问题的共识/解决方案,我也无法在委员会站点上的其他任何地方找到它。
我猜想,一个共识是,生命周期延长的临时对象的存储期限应等于它们所绑定的参考的存储期限,但是对于非生命周期延长的临时人员而言,尚不清楚。
特别是对我来说,目前尚不清楚在块范围内的语句中创建的临时变量是否具有(自动?)存储持续时间扩展到块末尾(如在同一点声明的自动变量一样),或者是否具有存储持续时间在全表达式结束时以其(默认)生存期结束。问题说明暗示了后者的情况。
说明中是否应该包含“ 暂定解决方案 ”,如果我解释正确或不正确,是否可以在其他地方找到该问题的“ 暂定解决方案 ”?
In addition, do the major compilers currently follow this consensus, either in a documented manner or de-facto?
See also older questions:
Edit for clarification:
I am not asking about the lifetime of the temporary, that is well-specified in the standard. I am asking about the storage duration. For example:
#include<new>
struct A {
operator A*() {
return this;
}
int i = 0;
// Some other stuff (and non-trivial destructor)
};
A* ptr1 = A();
A* ptr1_new = new(ptr1) A();
// Use new object through ptr1_new
A* ptr2 = A();
int main() {
A* ptr2_new = new(ptr2) A();
A* ptr3 = A();
A* ptr3_new = new(ptr3) A();
// Use new objects through ptr2_new and ptr3_new
}
Run Code Online (Sandbox Code Playgroud)
The question is whether the standard (committee) intends these placement-news to be well-defined or undefined behavior. After having thought about it for a while, I suppose that probably the intention is that all of them are undefined behavior, but I wanted to get a clear answer.
Another aspect is that the standard often refers to "objects of static/automatic/thread/dynamic storage duration" and that temporaries are objects, but without specification on how to determine to which of these four classes they belong.