What Does This Template Mean?

closed account (zb0S216C)
I've come across this class template:

1
2
3
4
5
template <template<class> class X>
class Something
{
   //...
};

I tried to instantiate it like so: ::Something<int> X; However, it gave an error and said it was a type mismatch.

Does anybody know what it means? Or even how to use it?

Thanks.

Wazzak
This is a template template parameter. I think it's the hardest thing to understand in C++; as a fun fact, I posed a question about it a few months ago (because I needed it and didn't know it existed) and no one could help me with it.

http://cplusplus.com/forum/general/46300/

You can use template template parameters to templatise over classes that are already templatised. For example, if you want to templatise a class for its std container type, you would do the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
template <template <typename elem, typename = std::allocator<elem> > class CONT>
class MyClass
{
public:
   CONT<double> mydoubles;
   CONT<int> myints;
};

int main()
{
   MyClass<std::vector> x; //now x.mydoubles is a vector of doubles
   MyClass<std::deque> y; //now y.mydoubles is a deque of doubles.
}


Hope that helps :-)
Last edited on
closed account (zb0S216C)
TheDestroyer wrote:
Hope that helps :-)

It does :)

I'll look through those posts.

Thanks for your reply :)

Wazzak
You're welcome! :-)
Topic archived. No new replies allowed.