In [except.ctor] the standard (N4140) guarantees that:
...destructors are invoked for all automatic objects constructed since the try block was entered...
However in the following example the empty output proves that the return value of function foo
is not destructed, although it has been constructed. Compiled using g++ (5.2.1) and clang++ (3.6.2-1) and with options -O0 -fno-elide-constructors -std=c++14
.
struct A { ~A() { cout << "~A\n"; } };
struct B { ~B() noexcept(false) { throw 0; } };
A foo() {
B b;
return {};
}
int main() {
try { foo(); }
catch (...) { }
}
Is this a bug both in g++ and clang++, or are function return values not considered automatic objects, or is it a loop hole in the C++ language?
In none of [stmt.return], [expr.call] or [dcl.fct] I have been able to find a clear statement whether a function return value is considered an automatic object. The closest hints I found are 6.3.3 p2:
...A return statement can involve the construction and copy or move of a temporary object...
and 5.2.2 p10:
A function call is an lvalue if the result type is an lvalue reference type or an rvalue reference to function type, an xvalue if the result type is an rvalue reference to object type, and a prvalue otherwise.