2

In C++11 (or C++) is it possible to pass a template type that is not fully specified. Specifically, I want to pass a type that does not have all of its template specifiers defined yet:

template <std::size_t N, typename ARRAYTYPE>
struct A {
  ARRAYTYPE<int, N> int_array;
};

int main() {
  A<10, std::array> my_a;
  return 0;
}

I know that simply redefining ARRAYTYPE = std::array<int, 10> would work, but would not let me utilize an ARRAYTYPE of different size anywhere in A:

template <std::size_t N, typename ARRAYTYPE>
struct A {
  ARRAYTYPE<int, N> int_array;
  ARRAYTYPE<int, 1> tiny_int_array;
};

Is this possible?

user
  • 7,123
  • 7
  • 48
  • 90

3 Answers3

6

It's called a "template template parameter", because it's a template parameter whose value is a template:

template <std::size_t N, template <typename, std::size_t> class ARRAYTYPE>
struct A {
  ARRAYTYPE<int, N> int_array;
  ARRAYTYPE<int, 1> tiny_int_array;
};
Steve Jessop
  • 273,490
  • 39
  • 460
  • 699
1

That would not be a type, but a template:

template <std::size_t N, template <typename, std::size_t> class ARRAY_TMPL>
struct A {
   ARRAY_TMPL<int, N> int_array;
};
David Rodríguez - dribeas
  • 204,818
  • 23
  • 294
  • 489
0

If I understand what you're trying to accomplish, you can pass the template itself as a template template parameter.

Community
  • 1
  • 1
Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111