C++ 这是谁的析构函数?

C++ 这是谁的析构函数?,c++,C++,在下面的代码中: #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&&) = defau

在下面的代码中:

#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的是谁的析构函数??我无法理解自动变量是按声明的相反顺序销毁的,因此指示的析构函数是
s1的析构函数

它在程序中的该点采用值
{',42}
的原因是
f
的返回值正在由move构造初始化;也就是说,
s1
被视为xvalue。这遵循[class.copy]/32中的规则:

当满足或将满足省略复制操作的条件时,除了源 对象是函数参数,要复制的对象由左值指定,重载分辨率为 选择用于复制的构造函数,该复制将首先执行,就像对象由右值指定一样


我似乎无法复制这个。你能告诉我们你使用的是哪个平台、编译器和编译命令行吗?除了第一行中的“42”之外,我复制了所有内容。你肯定在那里使用了C++11,整个“默认”业务。。。。语言:C++11创建时间:0秒前可见性:公共共享或嵌入源代码也无法复制-对我来说似乎很好。我得到的输出不是上面显示的结果。有趣的是,这种行为是由G++
-fno elide构造函数
选项触发的,该选项禁用了这种特定的优化。
 42 //???
 0  // s0 is destroyed
tool 42 // arg is destroyed
tool 42 // return value of f() is destroyed
tool 42 // s3 is destroyed