Re: a language question about conversion operators
Mycroft Holmes wrote:
I think I remember that template member functions do not trigger
user-defined conversions.
You mean, during template argument deduction user-defined conversions
are not considered? Yes, that's true. If that's not what you meant,
I don't have an answer.
The code below in fact does not compile. instead if I remove the cast
operator from C and let C derive from A, it succeeds.
Is the explanation correct?
Uh... Not sure what you mean.
TIA,
MH
template <typename T>
struct A {};
struct B
{
template <typename T>
bool operator==(A<T>) const
{
return true;
}
};
struct C // : public A<int>
{
operator A<int>() const
{
return A<int>();
}
};
int main()
{
C c;
B b;
return b == c;
}
I believe the inheritance establishes a much stronger "is-a" relationship
between 'C' and 'A<int>' than the presence of the conversion function.
And I guess it's the reason why the Standard says that the argument from
which the template arg is deduced can be the derived class of the specified
parameter.
In order to deduce the 'T' to instantiate the 'B::operator==' function,
the compiler looks at the argument. It's a C. Is it one of instantiations
of 'A'? No. Is its base classes one of instantiations of 'A'? No. Can't
do it.
Now, if 'C' derives from 'A<int>', process goes: [same...] Is its base
classes one of instantiations of 'A'? Yes! Which one? 'A<int>'. Now
we can match 'T' to 'int' to arrive to the identical template. The task
accomplished, 'T' is deduced as 'int'.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask