Re: a language question about conversion operators
"Mycroft Holmes" <m.holmes@nospam.it> wrote in message
news:OyRM3NhGIHA.1548@TK2MSFTNGP05.phx.gbl
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.
obviously, it would be technically possible for the compiler to add
another step that says: can C be convert to A<T1> for some fixed T1?
but if it does not,
sorry, I sent the message before finishing the sentence.
obviously, it would be technically possible for the compiler to add
another step that says: can C be convert to A<T1> for some fixed
non-ambiguous T1? if it can, then T1 is the type you require,
otherwise the conversion is impossible or ambiguous.
Conversion operator is not the only means by which C can be converted to
some other class. That class could also have a constructor from C:
template <typename T>
struct A {};
template <typename T>
void f(A<T>);
struct C {};
struct D : A<int> {
D(const C&);
}
C c;
f(c); // f<int>( D(c) ); ?
The constuctor and the conversion operator are pretty much doing the
same thing, only from different ends. Both are considered for implicit
type conversions by the compiler. It can be argued that conversion
constructors are much more common in practice than conversion operators.
But of course, to support such type inference, the compiler would have
to search the whole program for a class that may be constructed from C
and also may be somehow related to A<T> for some T. You get spooky
action at a distance: adding a new class may suddenly change the meaning
of a seemingly unrelated part of the program. You would also have to
#include every class that might have the slightest bearing on the
relationship between C and A<T> everywhere f<T> may be used, or risk ODR
violations (imagine two translation units calling f(c), one of which
includes D.h and the other E.h where class E also has constructor from
C).
--
With best wishes,
Igor Tandetnik
With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925