c++ - Visual C++ 2015 中右值的非常量引用初始化无效

标签 c++ visual-c++

大多数编译器 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 中通过

禁用扩展
  1. Open the project's Property Pages dialog box. For details, see How to: Open Project Property Pages.
  2. In the navigation pane, choose Configuration Properties, C/C++, Language.
  3. Modify the Disable Language Extensions property.

(来自docs)

然后,MSVC 将尝试遵循语言标准。

关于c++ - Visual C++ 2015 中右值的非常量引用初始化无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35939132/

相关文章:

c++ - 双数据类型的解释错误

c++ - 仅为C++文件生成标记文件(.h,.cpp,.cxx,.c)

c++ - 如何创建带有参数的操纵器?

.net - 如何将并发运行时与.NET代码混合使用?

c++ - 为什么 std::sort 找不到合适的(静态成员)函数重载?

c++ - 为什么 COM 在新线程中不起作用?

c++ - 打印时GDI折线部分输出

c++ - C++ 类的 C 包装器返回不正确的地址

python - 警告 LNK4197 : export 'PyInit_python_module_name' specified multiple times; using first specification

visual-c++ - Windows 10 上的模糊开发 C++