template instantiation logic ?

Alexandre Oliva oliva@dcc.unicamp.br
Sun Feb 8 13:45:00 GMT 1998


Marc ESPIE writes:

> class Gaussian_random_distribution: private Gaussian_random_generator, 
> 	public Random_distribution

> The strange thing is that, in order to get egcs to compile my code,
> instantiating the template as
> template Vector<double>& Vector<double>::operator=(Random_distribution &);
> is not enough !

If your code looks like:

Vector<double>& v;
Gaussian_random_distribution& d;
v = d;

overload resolution selects
Vector<double>::operator=<Gaussian_random_distribution>(Gaussian_random_distribution&),
because it is an exact match, no matter the appropriate assignment
operator for the base class is defined.

just define an explicit specialization for
Guassian_random_distribution that converts the argument to the base
type and calls the appropriate template specialization, or cast the
object of type Gaussian_random_distribution to Random_distribution&
before when you assign it to a Vector<double>, so that the static type
causes the overload resolution mechanism to select the template
specialization you intended it to.

-- 
Alexandre Oliva
mailto:oliva@dcc.unicamp.br mailto:aoliva@acm.org
http://www.dcc.unicamp.br/~oliva
Universidade Estadual de Campinas, SP, Brasil



More information about the Gcc mailing list