I have the following code:
#include <stdexcept>
#include <iostream>
struct ok {
int _n;
ok(int n) : _n(n) { std::cerr << "OK" << n << " born" << std::endl; }
~ok() { std::cerr << "OK" << _n << " gone" << std::endl; }
};
struct problematic {
~problematic() noexcept(false) { throw std::logic_error("d-tor exception"); }
};
ok boo() {
ok ok1{1};
problematic p;
ok ok2{2};
return ok{3}; // Only constructor is called...
}
int main(int argc, char **argv) {
try {boo();} catch(...) {}
}
I see that he destructor of ok{3} is not called, the output is:
OK1 born
OK2 born
OK3 born
OK2 gone
OK1 gone
Is it the expected behavior for C++14?
EDITs:
Compiling with gcc 6.3