The following code compiles fine under GCC and MSVC:
#include <string>
class C
{
public:
C(const std::string&) { }
};
void f()
{
std::string s;
const std::string& sr = s;
C c = C(sr);
}
Furthermore, if I replace that (entire) last line with this, both compilers accept it:
(C(sr));
However, if I replace that last line with this:
C(sr);
Compilers report the following:
GCC:
$ c++ --std=c++17 yo.cpp
yo.cpp: In function ‘void f()’:
yo.cpp:13:9: error: conflicting declaration ‘C sr’
C(sr);
^
yo.cpp:12:24: note: previous declaration as ‘const string& sr’
const std::string& sr = s;
MSVC:
cl /std:c++17 Source.cpp
Microsoft (R) C/C++ Optimizing Compiler Version 19.16.27043 for x64
Copyright (C) Microsoft Corporation. All rights reserved.
Source.cpp
C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.16.27023\include\xlocale(319): warning C4530: C++ exception handler used, but unwind semantics are not enabled. Specify /EHsc
Source.cpp(14): error C2040: 'sr': 'C' differs in levels of indirection from 'const std::string &'
Source.cpp(14): error C2512: 'C': no appropriate default constructor available
Source.cpp(3): note: see declaration of 'C'