Gil*_*ils 3 c++ oop gcc compilation
我花了一些时间才得到错误,但仍然不知道如何解决
正如代码中出现的那样,只有一个构造函数调用,但它调用了两次析构函数
代码:
struct A
{
A()
{
std::cout << "C'tor" << std::endl;
}
A(A&&)
{
std::cout << "M'tor" << std::endl;
}
A(const A&) = delete;
A& operator=(const A&) = delete;
A& operator=(A&&) = delete;
~A()
{
std::cout << "D'tor" << std::endl;
}
};
int main()
{
auto lambda = [](int i){
A a;
if (i == 0)
return std::move(a);
};
lambda(1);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:
C'tor
D'tor
D'tor
Run Code Online (Sandbox Code Playgroud)
怎么会这样?编译器至少应该为我生成一个警告吗?你怎么认为?