Re: Template with template parameter
Fred Kleinschmidt wrote:
[..]
Actually, what I have is slightly more complex. It's not really
a true copy constructor I need, since there are other parameters.
The constructor has 3 parameters;
Vct(const Vct&, int, int)
for various types of Vct<xxx>
I also want constructors passing primitive arrays:
Vct( const double *, int, int)
Vct(const float *, int, int)
... etc., plus int*, long*, long long*, etc.
And one that I can take a single variable instead of an array:
Vct( const double, int, int)
Vct(const float, int, int)
... etc., plus int, long, long long, etc.
You can define three different template constructors:
template<class T> class Vct {
...
template<class S> Vct(const Vct<S>&, int, int);
template<class S> Vct(const S*, int, int);
template<class S> Vct(S, int, int);
...
The problem with the third one is that it's a bit too generic, so
when you use
Vct<int> vi;
Vct<double> vd(vi, 1, 2);
it might pick the one with (S, int, int) instead of the one with
(const Vct<S>&, int, int) arguments. To guard against that you
could employ some SFINAE means, like the fact that S in the
(S, int, int) should be convertible to 'double', for example:
template<class S> Vct(S, int, int, double = S());
in which case it should choke when trying to deduce 'S' as Vct<>&
The same with 'const S*' -- an array of non-const elements may
not be a match. Experiment.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask