// gcc version 3.4.5 (mingw-special) class a { public: template<class T> operator T &() const { static int f; return f; // line # 10 } /* // unsafe workaround: operator a const &() const { return *this; } */ }; int main() { // : In member function `a::operator T&() const [with T = const a]': // :27: instantiated from here // :10: error: invalid initialization of reference of type 'const a&' from expression of type 'int' a const &aa = a(); // line # 27 /* // workaround: a const temp; a const &aa = temp; */ }
I've checked other versions of compiler, such as 3.4.6 and 4.X.X. Can someone check?
(In reply to comment #1) > I've checked other versions of compiler, such as 3.4.6 and 4.X.X. Can someone > check? > Sorry, I mean, I have *NOT*.
// Workaround: class a { public: template<class T> operator T &() const throw(typename ::boost::disable_if< ::boost::is_same<T, ref_t const> >::type *) { static int f; return f; } }; int main() { a const &aa = a(); // line # 27 int &i = aa; }
This works on the trunk.
(In reply to comment #3) > // Workaround: > class a > { > public: > template<class T> > operator T &() const > throw(typename ::boost::disable_if< > ::boost::is_same<T, ref_t const> >::type *) > { > static int f; > return f; > } > }; > > int main() > { > a const &aa = a(); // line # 27 > int &i = aa; > } > This workaround should not work, according to the standard 14.8.2.2 - All references in the function type of the function template to the corresponding template parameters are replaced by the specified template argument values. If a substitution in a template parameter or in the function type of the function template results in an invalid type, type deduction fails. [Note: The equivalent substitution in exception specifications is done only when the function is instantiated, at which point a program is ill-formed if the substitution results in an invalid type.] Type deduction may fail for the following reasons: However, it works on GCC. It is another bug in GCC.
Fixed long ago.