Templatise a class over itself?

Hello guys :),

Is this ever possible? I need it, but not so sure it would be supported in C++. I have inside my class a container for the class itself I'm creating. But I want to be free in choosing the container type using templates:

1
2
3
4
5
class MyClass
{
    vector<myClass> myVec;
    //rest removed for brevity
};


So I want to have the freedom of choosing the container to be a vector, deque, list, queue, or any other STL container using templates. Is that possible? is there a trick to do it?

Thank you for any efforts :)
The code you gave compiles as a charm on my system (gcc with code::blocks on ubuntu), and seems to work just fine (but please be careful with doing stuff such as
1
2
MyClass x;
x.myVec.push_back(x);


Can you explain what is the purpose of this class?
Last edited on
closed account (zwA4jE8b)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <vector>
#include <list>

template <class dtype>
class one
{
public:
	one(){two = new dtype;}
	dtype* two;
};

int main()
{
	one<std::vector<int>> a;
	a.two->push_back(5);

	one<std::list<float>> b;
	b.two->push_back(4);

	std::cout << a.two->front() << " " << b.two->front();
	std::cin.get();
	return 0;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <vector>
#include <list>

template <class dtype>
class one
{
public:
	one(){}
	dtype two;
};

int main()
{
	one<std::vector<int>> a;
	a.two.push_back(5);

	one<std::list<float>> b;
	b.two.push_back(4);

	std::cout << a.two.front() << " " << b.two.front();
	std::cin.get();
	return 0;
}
Last edited on
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
#include<iostream>
#include<vector>

class Weirdclass
{
  public:
  std::vector<Weirdclass> x;
  void printMe(int recDepth)
  { std::cout << "\n";
    for (int j=0; j<recDepth; j++)
        std::cout << "\t";
    std::cout << "I am nested in depth " << recDepth << "; My size is: " <<  this->x.size() << " My elements are: ";
    for (unsigned int i=0; i<this->x.size(); i++)
      this->x[i].printMe(recDepth+1);
  }
};

int main()
{ Weirdclass A;
  for (int i=0; i<5; i++)
    A.x.push_back(A);
  A.printMe(0);
  int i;
  std::cin >> i;

  return 0;
}
Last edited on
closed account (zwA4jE8b)
this also might help

http://cplusplus.com/doc/tutorial/templates/
Thank you for your fast answer.

But the code I wrote isn't what I'm looking for. I want to yet "templatise" over my container. So I expect something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
template<T>
class MyClass
{
    T<myClass> myVec;
    //rest removed for brevity
};

int main()
{
    MyClass<vector> x;
    MyClass<deque> y;
    MyClass<list> z;
    x.myVec.push_back(x);
    y.myVec.push_front(y);
    z.myVec.insert(z,...);
}


It looks stupid what I wrote, for sure. Because vector is not a class, but vector<double> is a class. I just wrote it that way to explain what I'm looking for.

Any ideas? :-)

Correction: code at list corrected
Last edited on
If this is impossible to be done, please let me know!
It does look impossible to me... what about simply calling the template class in a different manner?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include<iostream>
#include<vector>

template <class T>
class Weirdclass
{
  public:
  T a;
};


int main()
{ Weirdclass<std::vector<Weirdclass<std::vector<int> > > > x;

  return 0;
}
Thank you for your answer.

Not really... having ever an int inside isn't a good idea! sorry!

I need it to be of the same class! and as you have noticed, it appears to be an infinite series of template calls!

any other ideas?
@tition's original post: what is wrong with that code? It won't lead to recursion if that's what you think.

And yes it is impossible unless you get clever with C++11's variadic template parameters ;)
Thank you for your reply, L B.

But how do you think variadic templates parameters could help? I see the main problem here is the infinite recursions of template definitions required, or in other words the circular template defition that never ends!

container -> needs MyClass type
MyClass -> needs container type

I don't see how variadic template parameters could help. Please explain!!!!
If you can give a template parameter of the depth you can recurs to the point of your choosing.
The depth is unlimited. How can I give such a parameter?
Topic archived. No new replies allowed.