Template copy contructors

Here is something I am attempting which does not seem to work.

1
2
3
4
5
6
7
8
template <class T,int N>
class A{
        public:
                A(const A<T,N>&);
                A(const A<class M,int R>&); // This line throws an error
};
A<double,3>tmp1;
A<double,3>tmp2(tmp1);


The first copy constructor forces me to include identical N in the line A<double,3>tmp2(tmp1). I want to define a copy constructor for the case when the second template argument is not same for tmp1 and tmp2. Is this possible ?
probably not the best answer for this but where do expect <class M,int R> to be defined? I'm guessing c++ might not know.
First what kind of error did you get?

The first copy constructor forces me to include identical N in the line A<double,3>tmp2(tmp1). I want to define a copy constructor for the case when the second template argument is not same for tmp1 and tmp2


And second what do you mean here? A<double,3>tmp2(tmp1) with same tmp1 and tmp2 means A<double,3>3(3). It doesn't make sense.
You need to create a template where A<T,3> could be constructed by a A<T,4> class if I am guessing right?
You need to create a template where A<T,3> could be
constructed by a A<T,4> class if I am guessing right?


Precisely. I should be able to say

1
2
3
A<double> tmp2  = {/*Some operations involving objects of
                                    template<class T,int N>class all with 
                                    same T but potentially different N*/}


Is something like this possible in templates ?
Last edited on
*bump*

Any suggestions ?
1
2
3
4
5
6
7
8
template<class T, int N>
class foo{
public:
	foo(const foo &b);

	template<class R, int M>
	foo(const foo<R,M> &b); 
};
I have test a code snipset and puzzled my self. Two questions came up:

1) how can I define copy ctor outside class body
2) why M and N seems to swap?

The code I tested is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <typeinfo>
#include <iostream>

using namespace std;

template <class T, int N>
class mysequence {
    T memblock [N];
  public:
    mysequence();
    template <class U, int M> mysequence(mysequence<U,M>& ms)
    {cout << "Typeid is " << typeid(ms).name() << "\n"
    << " M=" << M << " N=" << N;
    cout << "Typeid of U is " << typeid(U).name() << "\n";
    cout << "Typeid of T is " << typeid(T).name() << "\n";
    }
    template <class U, int M> mysequence();
    void setmember (int x, T value);
    T getmember (int x);
};

template <class T, int N>
void mysequence<T,N>::setmember (int x, T value) {
  memblock[x]=value;
}

template <class T, int N>
T mysequence<T,N>::getmember (int x) {
  return memblock[x];
}

template <class T, int N>
mysequence<T,N>::mysequence()
{
    for(int i=0; i<N; i++)
        (*this).setmember(i,0);
    cout << " N=" << N;
//    return (*this);
}

int main () {
    const int N = 3;
    cout << "The number of elements for the 2 arrays (int, double) is " << N;
    mysequence <int,N> myints; //default ctor
    mysequence <double,N> myfloats; //default ctor
    mysequence<int,4> myints1(myfloats); //to test copy ctor
    return 0;
}


The code seems to be working. I think kishor8dm that this answers your question. If anyone has a point on my questions would be mostly welcome. I have puzzled my head down for some time now.

The definition producing error is:
1
2
3
4
//I can figure how to make this work!
template <class T, int N> mysequence<T,N>::
mysequence(template <class U, int M> mysequence<U,M>& ms) //here compiler kicks!
{...}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
template <class T, int N>
class mysequence {
	T memblock [N];
public:
	mysequence(); //default constructor

	template <class U, int M> 
	mysequence(const mysequence<U,M>& ms); //don't forget the const

	template <class U, int M> 
	mysequence(); //¿what's this?

};

template <class T, int N> //the class is a template 
template <class U, int M> //the method is a template
mysequence<T,N>::mysequence(const mysequence<U,M>& ms){
//...
}


2) why M and N seems to swap?
¿what do you mean?
Yup, looks like this solves my problem.
OK,

thanks that indeed works fine!

2) why M and N seems to swap?

In my example I think that mysequence <double,3> myfloats; so in copy constructor for mysequence<int,4> myints1 I guess:
T = int (as for myints1), N = 3 (as defined in template instance
U = double (as for myfloats), M = 4.cuting t
When executing the example N appears to be 4 and M = 3, thus swapping!

P.S. in default ctor N = 3 (as ti should).

Any ideas?
Topic archived. No new replies allowed.