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?
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?
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.
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!!!!