大多数编译器 detect the error在下面的代码中。
struct Foo
{
int i;
};
Foo create_foo()
{
return { 42 };
}
int main()
{
Foo& my_foo = create_foo(); // This should trigger a compiler error.
my_foo.i = 33; // Undefined behaviour!
}
我的机器上的 Visual C++ 2015 没有。是否有编译器标志/警告级别或我可以设置的东西以使 VC2015 检测到错误?
最佳答案
MSVC 有一个允许这样做的扩展,因此默认情况下,这不是一个错误。您可以使用标记 /Za
或在 IDE 中通过
- Open the project's Property Pages dialog box. For details, see How to: Open Project Property Pages.
- In the navigation pane, choose Configuration Properties, C/C++, Language.
- Modify the Disable Language Extensions property.
(来自docs)
然后,MSVC 将尝试遵循语言标准。
关于c++ - Visual C++ 2015 中右值的非常量引用初始化无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35939132/