template instantiation logic ?
Marc ESPIE
Marc.Espie@liafa.jussieu.fr
Sun Feb 8 15:56:00 GMT 1998
On Sun, Feb 08, 1998 at 06:10:11PM -0200, Alexandre Oliva wrote:
> 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.
Hum... I thought about that, but I'm not convinced. I know the classic trick
where you avoid building myriads of instances of list<A *> by playing with
types and just converting everyone to list<void *>, but I believe there is
a big difference there... There is no inheritance relation between A * and
void *, whereas there is a clean inheritance relation between
Gaussian_random_distribution and Random_distribution. Moreover, the template
instance only uses the Random_distribution::operator() method,
which is pure virtual.
Hence the compiler (IMHO) has everything it needs to know that inheritance
WILL work and should be able to optimize the call to use
Random_distribution directly: the end result will be the same, and
everything will be `as if' the appropriate specialization had been called.
Looks like a useful optimization to me.
--
Marc Espie
More information about the Gcc
mailing list