template instantiation logic ?
Marc ESPIE
Marc.Espie@liafa.jussieu.fr
Sun Feb 8 10:01:00 GMT 1998
I've been experimenting with template members, and I've got some problems.
I won't give the whole code, as this would be rather long.
Let's say I have a template<class T>Vector which acts as an array for
my own use.
I want to be able to instantiate this vector from a function-like object
acting as a generator.
This gives:
template<class T>class Vector
{
public:
// ... other things
template<class U>Vector<T>& operator=(U& d);
};
and that operator is defined elsewhere as:
template<class T>template<class U>Vector<T>& Vector<T>::operator=(U& d)
{
Vector<T> &v = *this;
for (size_t i = 0; i < length(); i++)
v[i] = d();
return *this;
}
so far, so good.
Now comes the interesting part. Among my generators, I've got random
numbers, declared as follows:
class Random_distribution
{
public:
virtual double operator()() = 0;
};
class Gaussian_random_distribution: private Gaussian_random_generator,
public Random_distribution
{
double mean, sigma;
public:
Gaussian_random_distribution(double m=0.0, double s=1.0);
double operator()();
};
(and corresponding definitions)
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 !
I need to use:
template Vector<double>&
Vector<double>::operator=(Gaussian_random_distribution &);
Is this normal behavior ?
I would have thought inheritance to be enough to cover this case (as per
Stroustrup's 3rd edition, section 13.3.2, step 3 `do overload resolution
for this set of functions, plus any ordinary functions as for
ordinary functions', especially as the
template Vector<double>& Vector<double>::operator=(Random_distribution &)
can definitely cover the more specialised case.
--
Marc Espie
More information about the Gcc
mailing list