我已经写了下面的代码......
#include <iostream>
using namespace std;
struct Error {
Error() { cout << "Constructor called\n"; }
Error(const Error&) { cout << "Copy Constructor called\n"; }
};
void test() {
throw Error();
}
int main() {
try {
test();
}
catch (Error e) {
}
}
我在 C++17 及更高版本上获得以下输出...
Constructor called
Copy Constructor called
甚至 C++17 保证的复制省略也说"Copy elision is mandatory in Throwing and catching exceptions by value"
。我得到了"Copy Constructor called"
as 输出。
我是否遗漏了什么?