Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ variadic templates with type and non-type argument mixing for recursive inheritance

I would like to achieve this:

Foo<int, 5, double, 7> foo_1;
Foo<char, 7> foo_2;

foo_1.Foo<int>::value[4] = 1;
foo_2.Foo<char>::value[1] = 'x';

(This is an oversimplified example, Foo would do much more than this.)

How can I do that with variadic templates?

TLDR;

I know that variadic templates can be used in this way if exclusively types or non-types are used:

template <typename ...T>
struct Foo;

template<typename T, typename ...Args>
struct Foo<T, Args...> : Foo<T>, Foo<Args...> {
    Foo(T t, Args... args) : Foo<T>(t), Foo<Args...>(args...) { }
};

template<typename T>
struct Foo<T> {
    T value;

    Foo<T>(T v) : value(v) {}
};

Foo<int, double, char> f(7, 88.1, 'x');
f.Foo<double>::value = 5;

But I do not whether it is possible to pair and mix type and non-type template arguments using variadic templates.

like image 949
Tibor Takács Avatar asked Nov 07 '25 17:11

Tibor Takács


1 Answers

It's not possible. You'll have to settle for one of the following:

Foo<Bar<int, 5>, Bar<double, 7>> foo_1;
Foo<int, Bar<5>, double, Bar<7>> foo_1;
// ...?

If the values are always integral, you could also try this:

Foo<int[5], double[7]> foo_1;

And then extract elemenet types & extents from each argument.

like image 61
HolyBlackCat Avatar answered Nov 09 '25 10:11

HolyBlackCat