How to create a class with templated class variables as template arguments

Viewed 37

I have started learning templates in c++, and i would like to make a class that has a variable number of variables of a templated class. In other words, I have a class A

template<typename T>
class A;

Now I would like to make a class B such that the class would be instantiated like so:

B<A<int>(1), A<float>(2.4f), A<bool>(false),....>();

I managed to make a pretty strightforward templated function that does the same thing, like this:

template<typename C>
void test(A<C> args...) {}

test(A<int>(1), A<bool>(true),...) // works fine

Is it possible to do it with a class, and if so, how? Thanks in advance!

1 Answers

What you're searching for is likely already provided by the standard class std::tuple<>, albeit with a slightly different syntax:

auto b = std::tuple<int, float, bool>(1, 2.4f, false);
// or
auto b2 = std::make_tuple(1, 2.4f, false);

Then you can access the elements using std::get<i>(b).

Related