Template with template parameter

F

Fred Kleinschmidt

I have a template::

template <class V> class Vct {
public:
Vct(V);
};

template <class V>
Vct<V>::Vct( V vin ) {
/*...*/
}

Now I would like to add a copy constructor that
takes as its input any arbitrary instance of a Vct<something>.
For example,
double d = 0.0;
int i = 0;
Vct<double> x ( d );
Vct<int> iy( i );

/* Now I want to make a (double) copy of iy */
Vct<double> z( iy );

/* ... and a (int) copy of x*/
Vct<int> im( x);

Can it be done? How do I define such a constructor
as a template?
 
T

terminator

I have a template::

template <class V> class Vct {
public:
Vct(V);

};

template <class V>
Vct<V>::Vct( V vin ) {
/*...*/

}

Now I would like to add a copy constructor that
takes as its input any arbitrary instance of a Vct<something>.
For example,
double d = 0.0;
int i = 0;
Vct<double> x ( d );
Vct<int> iy( i );

/* Now I want to make a (double) copy of iy */
Vct<double> z( iy );

/* ... and a (int) copy of x*/
Vct<int> im( x);

Can it be done? How do I define such a constructor
as a template?

just nest a template in your template:

template <class V> class Vct {
public:
Vct(const V&);

Vct(const Vct& v){/*and go on*/};

template <class Init>//copy ctor:
Vct(const Vct<Init>& init){/*and go on*/};
//assignment:
const Vct& operator=(const Vct& v){/*and go on*/return *this;};

template <class Assign>
const Vct& operator=(const Vct<Assign>& assign){/*and go on*/
return *this;};


};

regards,
FM.
 
F

Fred Kleinschmidt

terminator said:
just nest a template in your template:

template <class V> class Vct {
public:
Vct(const V&);

Vct(const Vct& v){/*and go on*/};

template <class Init>//copy ctor:
Vct(const Vct<Init>& init){/*and go on*/};
//assignment:
const Vct& operator=(const Vct& v){/*and go on*/return *this;};

template <class Assign>
const Vct& operator=(const Vct<Assign>& assign){/*and go on*/
return *this;};


};

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.
 
V

Victor Bazarov

Fred said:
[..]
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,291
Messages
2,571,455
Members
48,132
Latest member
KatlynC08

Latest Threads

Top