473,385 Members | 1,919 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Template with template parameter

I have a template::

template <class Vclass 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?
--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Aero Stability and Controls Computing

Jul 20 '07 #1
3 1523
On Jul 20, 7:03 pm, "Fred Kleinschmidt"
<fred.l.kleinmschm...@boeing.comwrote:
I have a template::

template <class Vclass 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?
--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Aero Stability and Controls Computing
just nest a template in your template:

template <class Vclass 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.

Jul 20 '07 #2

"terminator" <fa***********@gmail.comwrote in message
news:11*********************@r34g2000hsd.googlegro ups.com...
On Jul 20, 7:03 pm, "Fred Kleinschmidt"
<fred.l.kleinmschm...@boeing.comwrote:
>I have a template::

template <class Vclass 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?
--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Aero Stability and Controls Computing

just nest a template in your template:

template <class Vclass 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.

--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Aero Stability and Controls Computing


Jul 20 '07 #3
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 Tclass Vct {
...
template<class SVct(const Vct<S>&, int, int);
template<class SVct(const S*, int, int);
template<class SVct(S, int, int);
...

The problem with the third one is that it's a bit too generic, so
when you use

Vct<intvi;
Vct<doublevd(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 SVct(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
Jul 20 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Lionel B | last post by:
Greetings. The following code compiles ok and does what I'd expect it to do: ---------- START CODE ---------- // test.cpp
8
by: Tony Johansson | last post by:
Hello Experts! What does this mean actually. If you have a template with a type and non-type template argument, say, like this template<typename T, int a> class Array {. . .}; then A<int,...
2
by: Siegfried Weiss | last post by:
Hi guys, i give up finding a solution by reading or by trial & error. Hope, YOU can help me! (Sorry for my rather long posting.) Stroustrup says, that templates could be declared with - type...
3
by: Erik Wikström | last post by:
I've been trying for a while now to understand how template template parameters work. But I just can't wrap my head around it and was hoping that someone might help me. As best I can figure the...
1
by: Kai-Uwe Bux | last post by:
Hi folks, I would like to know which clause of the standard rules out this: template < typename eval > struct recursive_template { typedef typename eval::enum_type Enum;
8
by: Jess | last post by:
Hi, I have a template function that triggered some compiler error. The abridged version of the class and function is: #include<memory> using namespace std; template <class T>
3
by: Anonymous | last post by:
Could someone please explain template template classes, showing: 1). Why they are needed / i.e what problem do they solve ? 2). A simple example I have read various articles etc, but it still...
2
by: Gary Nastrasio | last post by:
I'm currently reading Andrei Alexandrescu's book "Modern C++ Design" and I'm a bit confused by one bit of template syntax in chapter 1. Here is a code example: template <class CreationPolicy>...
4
by: David Sanders | last post by:
Hi, I have a class with an integer template parameter, taking values 1, 2 or 3, and a function 'calc' in that class which performs calculations. Some calculations need only be performed if the...
2
by: ndbecker2 | last post by:
On upgrading from gcc-4.1.2 to gcc-4.3, this (stripped down) code is now rejected: #include <vector> #include <iostream> template<typename T, template <typename Aclass CONT=std::vector>...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.