但是,标准C 11是否保证传递给函数的临时对象在函数结束后(之前没有)被销毁?
工作草案,编程语言标准C 2016-07-12:http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/n4606.pdf
§ 12.2 Temporary objects
§ 12.2 / 5
There are three contexts in which temporaries are destroyed at a
different point than the end of the full expression. The first context
is when a default constructor is called to initialize an element of an
array with no corresponding initializer (8.6). The second context is
when a copy constructor is called to copy an element of an array while
the entire array is copied (5.1.5,12.8). In either case,if the
constructor has one or more default arguments,the destruction of
every temporary created in a default argument is sequenced before the
construction of the next array element,if any. The third context is
when a reference is bound to a temporary.
也:
§ 1.9 / 10
A full-expression is an expression that is not a subexpression of
another expression. [ Note: in some contexts,such as unevaluated
operands,a syntactic subexpression is considered a full-expression
(Clause 5). — end note ] If a language construct is defined to produce
an implicit call of a function,a use of the language construct is
considered to be an expression for the purposes of this definition. A
call to a destructor generated at the end of the lifetime of an object
other than a temporary object is an implicit full-expression.
Conversions applied to the result of an expression in order to satisfy
the requirements of the language construct in which the expression
appears are also considered to be part of the full-expression.
这是否意味着标准C 11保证传递给函数的临时对象不会在函数结束之前被破坏 – 并且恰好在完整表达式的末尾?
#include <iostream> using namespace std; struct T { T() { std::cout << "T created \n"; } int val = 0; ~T() { std::cout << "T destroyed \n"; } }; void function(T t_obj,T &&t,int &&val) { std::cout << "func-start \n"; std::cout << t_obj.val << "," << t.val << "," << val << std::endl; std::cout << "func-end \n"; } int main() { function(T(),T(),T().val); return 0; }
输出:
T created T created T created func-start 0,0 func-end T destroyed T destroyed T destroyed
我们可以说被摧毁的T总会在func-end之后吗?
还有这个:
function(T(),T().val);
总是等于这个:
{ T tmp1; T tmp2; T tmp3; function(tmp1,tmp2,tmp3.val); }