#include <iostream>
struct S {
S() { throw 42; }
S(S&&) { std::cout << "I am nontrivial" << std::endl; }
};
S foo() {
try {
return {};
} catch (int) {
std::cout << "caught by foo\n";
throw;
}
}
int main() {
try {
S s = foo();
} catch (int) {
std::cout << "caught by main\n";
}
}
Therefore, I would expect that the constructor of S should be called in the context of the definition of s, and foo should not have the opportunity to catch the exception. Put another way, conceptually, the last thing foo() does before it returns is to create a prvalue that says to value-initialize the S object (whenever that might finally be required); it doesn't get to actually call the constructor.
Is the standard clear enough that the behaviour of GCC and Clang in this case is actually the intended behaviour? I think that it is not clear.