Why catch block is ignored, and std::terminate is invoked? [duplicate]

1 week ago 8
ARTICLE AD BOX

Look at this program (godbolt):

#include <cstdio> struct Foo { ~Foo() { printf("~Foo\n"); } }; void foo(Foo) { } int main() { foo(Foo()); }

Based on my previous question, this program should not materialize the Foo() in main, but just use it as an initializer when calling foo. So there should be only one instance of Foo, the parameter of foo(). However, with MSVC, the code prints

~Foo ~Foo

so there were two instances created, supposedly one for the temporary in main, and one for foo()'s parameter (note: with gcc, only one ~Foo line is printed).

Is this standard conformant?

(Note: if I add an empty copy constructor in Foo, then only one ~Foo line is printed).

cigien's user avatar

cigien

61.3k11 gold badges86 silver badges124 bronze badges

asked Jun 13, 2025 at 9:57

geza's user avatar

5

Read Entire Article