The following works fine (as one would expect):
struct X {};
struct A
{
operator X const& ()
{
static const X value{};
return value;
}
};
int main()
{
A a;
X x = a;
}
But this is not so clear:
template<typename T>
struct X {};
struct A
{
template<typename T>
operator X<T> const& ()
{
static const X<T> value{};
return value;
}
};
int main()
{
A a;
X<int> x = a;
}
GCC 4.9 says error: conversion from ‘A’ to non-scalar type ‘X<int>’ requested
whereas clang 3.4 has no problems with it.
If you remove the const
or the &
from the conversion function or if you write X<int> const &x = a
then GCC is happy, too.
So GCC only fails to find the conversion function if the target type is a const &
to a template class and you request a conversion to a non-const &
object of that class. Is this the correct behavior? I tried to read the standard but the overloading rules are quite confusing to me.