函数调用中的隐式析构函数执行
Implicit destructor execution in function calling
我想知道标准对下面这段代码的看法。 string
临时对象的析构函数可以在调用 printPointer
之前执行吗?
p.s。 VS2010 编译器不会抱怨此代码并且可以正常工作。
void printPointer(const string* pointer)
{
cout << *pointer << endl;
}
const string* func(const string& s1)
{
return &s1;
}
int main()
{
printPointer(func("Hello, World!!!"));
}
Can string
destructor of temporary object be executed before calling printPointer
?
否,因为临时对象将作为评估 完整表达式 的最后一步被销毁,其中包含创建它们的点,这意味着它会一直存在直到调用printPointer()
结束。
来自标准#12.2/4 临时对象
[class.temporary]:
Temporary objects are destroyed as the last step in evaluating the full-expression ([intro.execution]) that (lexically) contains the point where they were created.
和#12.2/6 临时对象
[class.temporary]:
A temporary object bound to a reference parameter in a function call ([expr.call]) persists until the completion of the full-expression containing the call.
我想知道标准对下面这段代码的看法。 string
临时对象的析构函数可以在调用 printPointer
之前执行吗?
p.s。 VS2010 编译器不会抱怨此代码并且可以正常工作。
void printPointer(const string* pointer)
{
cout << *pointer << endl;
}
const string* func(const string& s1)
{
return &s1;
}
int main()
{
printPointer(func("Hello, World!!!"));
}
Can
string
destructor of temporary object be executed before callingprintPointer
?
否,因为临时对象将作为评估 完整表达式 的最后一步被销毁,其中包含创建它们的点,这意味着它会一直存在直到调用printPointer()
结束。
来自标准#12.2/4 临时对象 [class.temporary]:
Temporary objects are destroyed as the last step in evaluating the full-expression ([intro.execution]) that (lexically) contains the point where they were created.
和#12.2/6 临时对象 [class.temporary]:
A temporary object bound to a reference parameter in a function call ([expr.call]) persists until the completion of the full-expression containing the call.