Non-const reference bound to temporary, Visual Studio bug?

后端 未结 2 800
慢半拍i
慢半拍i 2020-11-22 05:08

I ran into this while compiling some portable code in gcc. Basically this strange code compiles in Visual studio which really just blows my mind:



        
2条回答
  •  日久生厌
    2020-11-22 05:43

    This is old extension to Visual Studio, the only reference I could find on the Microsoft site was this bug report: Temporary Objects Can be Bound to Non-Const References, which has the following example code:

    struct A {};
    
    A     f1();
    void f2(A&);
    
    int main()
    {
        f2(f1()); // This line SHALL trigger an error, but it can be compiled without any     errors or warnings.
    }
    

    One of the responses notes:

    There is a level 4 warning (level 4 warning are enabled if you pass /W4 to the compiler) for it

    This blog post: Visual C++ is so Liberal which covers this extension notes that:

    Using Disable Language Extensions (/Za) makes it an error:

提交回复
热议问题