samedi 3 novembre 2018

MSVC binds non-const lvalue reference to an rvalue/temporary

MSVC lets you bind non-const lvalue reference to an rvalue/temporary. I have tested VS2008, VS2010, VS2012, VS2015, and multiple VS2017 all of them fine with the code below while Clang 3-8 and GCC 4-9 gives an error.

struct dummy {};
template <typename T> void foo(T&) {}
template <typename T> T bar() { return T(); }

int main()
{
    //int& i = 1;       // 1. Fails as expected on all compilers
    //foo(1);           // 2. Fails as expected on all compilers
    //foo(bar<int>());  // 3. Fails as expected on all compilers
    dummy& d = dummy(); // 4. OK on MSVC
    foo(dummy());       // 5. OK on MSVC
    foo(bar<dummy>());  // 6. OK on MSVC
}

https://godbolt.org/z/1u5EVD

Are not all those cases forbidden in [dcl.init.ref]/5.2? Am I missing something? Somebody should have noticed that MSVC violates the basics of the standard for decades.

Aucun commentaire:

Enregistrer un commentaire