我最近在一个在线编译器上测试了下面这个简单的程序。在此处查看现场演示。它编译得很好并给出了预期的输出,但是当我在 Dev C++ IDE 上测试它时,它在编译过程中失败了。
这是我的程序:
#include <iostream>
class Test
{
int s=9;
public:
int get_s()
{
return s;
}
};
int main()
{
Test s;
Test& t{s}; // error in C++11 but not in C++14 why???
std::cout<<t.get_s();
}
它给了我以下错误:
[Error] invalid initialization of non-const reference of type 'Test&' from an rvalue of type '<brace-enclosed initializer list>'
我还在代码块 13.12 IDE 上尝试过它,它给了我与 Dev C++ 相同的错误。
这是一个新的 C++14 特性吗?为什么它在 C++11 编译器中不起作用?