This program is compiled by clang:
#include <mutex>
int main() {
std::mutex mtx;
const std::lock_guard<std::mutex>& lock(mtx);
return 0;
}
Other major compilers refuse it (I have tried gcc, msvc, and icc). This is an error message from gcc:
error: invalid initialization of reference of type ‘const
std::lock_guard<std::mutex>&’ from expression of type ‘std::mutex’
Others give similar errors.
Is clang right or wrong? Can this be reproduced with a simpler example not involving library classes? I have tried but to no avail.
Edit this seems to be the minimal reproduction:
struct A {};
struct X
{
explicit X(A&) {};
};
int main()
{
A a;
const X& x(a);
}
Interestingly, an int in place of A does trigger the error message in clang (which is why I could not reproduce this initially).