在下面的代码中:
我得到了以下输出(我用我的推理进行了注释):
谁的析构函数会输出42?我不理解。
#include <string>
#include <iostream>
struct S {
std::string a;
int b;
S(const S&) = default;
S(S&&) = default;
S& operator=(const S&) = default;
S& operator=(S&&) = default; // not required here but should be added for completeness
~S() {
std::cout << a << " " << b << std::endl;
}
};
S f(S arg) {
S s0{};
S s1(s0); //s1 {s0}; in the book
s1 = arg;
return s1;
}
int main()
{
S s3{"tool",42};
f(s3);
}
我得到了以下输出(我用我的推理进行了注释):
42 //???
0 // s0 is destroyed
tool 42 // arg is destroyed
tool 42 // return value of f() is destroyed
tool 42 // s3 is destroyed
谁的析构函数会输出42?我不理解。