franky.backeljauw@ua.ac.be wrote in message news:<Pine.GSO.4.50.0307040128160.6859-100000@hmacs.ruca.ua.ac.be>...[color=blue]
> Hello,
>
> I have a problem with using a copy constructor to convert an object of a
> templated class to object of another templated class. Let me first
> include the code (my question is below):[/color]
[color=blue]
> template<class T> class tempo;
>
> template<class T>
> class delo
> { /* snip */
> public:
> delo () : a(0), b(0) {}
> delo ( T c, T d ) : a(c), b(d) {}
>
> void evaluate( tempo<T>& c );
> };[/color]
[color=blue]
> template <class T>
> class tempo
> { /* snip */
> public:
> tempo () : value(0) {}
> tempo ( T init ) : value(init) {}
> tempo ( delo<T> d ) { d.evaluate(*this); }
> };
> /*snip/[/color]
[color=blue]
> The class "delo" is used to delay the computation (compute, in this
> example an addition) until it needs to be executed, e.g. when a left-side
> object is given or when the result is requested for instance using cout.[/color]
This all looks simple and reasonable. However, the compiler can't assume
things are this simple. There might be template specializations of tempo
(e.g. when T==float: tempo<float>) where there is an additional ctor
(e.g. tempo( delo<double> d ) ). Now if the compiler has a delo<double>
it would be ambiguous which tempo<T> to create. Worse, unless the compiler
instantiates delo<T> for all Ts, it doesn't actually know which delo<T>
instances have a delo<double>. And "all Ts" is an infinitely large set.
Therefore the compiler isn't required or even allowed to take implicit
template instantiations into account when doing implicit casts. As
pointed out, if you specify the type T, it will succeed.
Regards,
--
Michiel Salters