The current diagnostic assumes the reference binding fails because the
reference is non-const, but it can also fail if the rvalue is volatile.
Use the current diagnostic for non-const cases, and a modified
diagnostic otherwise.
gcc/cp/ChangeLog:
PR c++/100635
* call.c (convert_like_internal): Print different diagnostic if
the lvalue reference is const.
gcc/testsuite/ChangeLog:
* g++.dg/conversion/pr100635.C: New test.
"type %qH to a value of type %qI",
totype, next->type);
}
- else
+ else if (!CP_TYPE_CONST_P (TREE_TYPE (ref_type)))
error_at (loc, "cannot bind non-const lvalue reference of "
"type %qH to an rvalue of type %qI", totype, extype);
+ else // extype is volatile
+ error_at (loc, "cannot bind lvalue reference of type "
+ "%qH to an rvalue of type %qI", totype,
+ extype);
}
else if (!reference_compatible_p (TREE_TYPE (totype), extype))
{
--- /dev/null
+// PR c++/100635
+// { dg-do compile }
+// { dg-additional-options "-Wno-volatile" { target c++2a } }
+
+struct S { };
+volatile S v();
+const volatile S& svol = v(); // { dg-error "cannot bind lvalue reference of type 'const volatile S&' to an rvalue of type 'volatile S'" }
+
+#if __cplusplus >= 201103L
+volatile int&& declvol();
+const volatile int& voli = declvol(); // { dg-error "cannot bind lvalue reference of type 'const volatile int&' to an rvalue of type 'volatile int'" "" { target c++11} }
+#endif