2

Consider the snippet below:

#include<tuple>

double refd = 5.0;
int refi = 1;
decltype(auto) foo(){return std::tuple<double&,int&>{refd,refi};}

auto&[d,i] = foo(); //msvc:pass, gcc:fail, clang:fail

Available on compiler explorer

In the case of gcc/clang, the error is:

error: cannot bind non-const lvalue reference of type 'std::tuple<double&,int&>&' to an rvalue of type 'std::tuple<double&, int&>'

However, MSVC does not complain and compiles just fine.

Which one is correct ?

David G
  • 94,763
  • 41
  • 167
  • 253
Naebaf
  • 321
  • 1
  • 8
  • 3
    Disable extension on MSVC by `/Za` and the code fails too. – rafix07 May 22 '19 at 21:25
  • You're doing `auto& x = foo();` Does that help answer your question? – Barry May 22 '19 at 21:26
  • @rafix07 If you have the time, explain what `/Za` exactly does, and this should be an answer. – πάντα ῥεῖ May 22 '19 at 21:27
  • 1
    @Barry I closed it to the canonical. – NathanOliver May 22 '19 at 21:33
  • @NathanOliver Weird. I'm pretty sure that target includes every permutation I searched for but couldn't find it anyway. Nice! – Barry May 22 '19 at 21:35
  • 1
    Prefer `/permissive-` to `/Za` if you can; it makes MSVC even more standards compliant – Justin May 22 '19 at 21:37
  • 2
    @Barry It's a tough one to find. I actually used it in an answer of mine with the link text containing evil so when I need it I just search my answers for evil and it pops right up :) – NathanOliver May 22 '19 at 21:37
  • 1
    An insight in what's happening: [cppinsights](https://cppinsights.io/lnk?code=I2luY2x1ZGU8dHVwbGU+Cgpkb3VibGUgcmVmZCA9IDUuMDsKaW50IHJlZmkgPSAxOwpkZWNsdHlwZShhdXRvKSBmb28oKXtyZXR1cm4gc3RkOjp0dXBsZTxkb3VibGUmLGludCY+e3JlZmQscmVmaX07fQoKYXV0byYmW2QsaV0gPSBmb28oKTsgLy9tc3ZjOnBhc3M=&insightsOptions=cpp17&std=cpp17&rev=1.0) – JVApen May 22 '19 at 21:44

0 Answers0