Muhammedogz
Muhammedogz

Reputation: 792

How could use Template Class for a container of container?

I want to use my class with the template. in main:

int main(void)
{
   HexAdapter<vector> foo;
   // maybe?
   // HexAdapter<vector<Cell>> foo;

   return 0;

}

I tried something like this

template <typename T>
class HexAdapter
{
public:
    HexAdapter();

private:
    T<T<Cell>> hexCells;

};

For the reason of this, Normally I used like this vector<vector<Cell>> hexCells But I want to work with all STL Containers with random access iterator.

Upvotes: 2

Views: 132

Answers (1)

jfMR
jfMR

Reputation: 24738

You can achieve the HexAdapter<std::vector> syntax by relying on a template template parameter, i.e., a template parameter which is, in turn, a class template (or an alias template):

struct Cell { /* ... */ };

template<template<typename...> class Cont>
class HexAdapter {
   Cont<Cont<Cell>> hexCells;
   /* ... */
};

The template argument to the class template HexAdapter (i.e., the argument to the Cont parameter) must be a class template itself (e.g., std::vector or std::deque):

auto main() -> int {
   HexAdapter<std::vector> foo;
   HexAdapter<std::deque> bar;
}

Upvotes: 2

Related Questions