https://gcc.gnu.org/bugzilla/show_bug.cgi?id=79035
Bug ID: 79035 Summary: Rejects valid code due to failure to use user const conversion template operator Product: gcc Version: 6.3.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: vz at quantitativesystems dot com Target Milestone: --- In the following example, the conversion int value2 = cdc fails, tested in many versions of g++: struct DualConverter { template<typename T> operator T &(); template<typename T> operator const T &() const; }; void instantiate(DualConverter &mdc, const DualConverter &cdc) { int &modif1 = mdc; const int &const1 = mdc; int value1 = mdc; const int &const2 = cdc; int value2 = cdc; // The compiler can't perform this conversion } g++ 6.3.0 gives this error: src/f5.cpp: In function ‘void instantiate(DualConverter&, const DualConverter&)’: src/f5.cpp:11:18: error: cannot convert ‘const DualConverter’ to ‘int’ in initialization int value2 = cdc; ^~~ The older g++ 4.8.3 gives a perhaps more informative error: src/f5.cpp: In function ‘void instantiate(DualConverter&, const DualConverter&)’: src/f5.cpp:11:18: error: invalid user-defined conversion from ‘const DualConverter’ to ‘int’ [-fpermissive] int value2 = cdc; ^ src/f5.cpp:2:26: note: candidate is: DualConverter::operator T&() [with T = int] <near match> template<typename T> operator T &(); ^ src/f5.cpp:2:26: note: no known conversion for implicit ‘this’ parameter from ‘const DualConverter*’ to ‘DualConverter*’ src/f5.cpp:11:18: error: passing ‘const DualConverter’ as ‘this’ argument of ‘DualConverter::operator T&() [with T = int]’ discards qualifiers [-fpermissive] int value2 = cdc; All versions of clang tested including 3.9 correctly compile this example. I suspect this other bug is related: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63217 Because of the multiple conversions that work, it seems the compiler fails to make the conversion of const references to value when given the option to convert through the non-const conversion operator template. I can not reproduce the bug without the conversions being templates. Template user conversion operators are unique in the language with respect to their capabilities. Thanks.