简体   繁体   English

C++ 具有类型和非类型参数混合的可变参数模板,用于递归 inheritance

[英]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.) (这是一个过于简单的例子, Foo的作用远不止于此。)

How can I do that with variadic templates?如何使用可变参数模板做到这一点?

TLDR; 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.但我不知道是否可以使用可变参数模板配对和混合类型和非类型模板 arguments。

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.然后从每个参数中提取元素类型和范围。

But I do not whether it is possible to pair and mix type and non-type template arguments using variadic templates.但我不知道是否可以使用可变参数模板配对和混合类型和非类型模板 arguments。

Not with an alternate variadic template.不使用备用可变参数模板。 But you can wrap couples of types/sizes.但是您可以包装几种类型/尺寸。

Suppose you have a Bar class as follows假设你有一个Bar class 如下

template <typename T, std::size_t N>
struct Bar
 { std::array<T, N>  value; };

using class specialization you can have a variadic Bar so a variadic list of types and a variadic list of sizes使用 class 专业化,您可以拥有一个可变参数Bar所以一个可变参数类型列表一个可变参数大小列表

template <typename...>
struct Foo;

template <typename ... Ts, std::size_t ... Ns>
struct Foo<Bar<Ts, Ns>...> : public Bar<Ts, Ns>...
 { };

You can use it as follows您可以按如下方式使用它

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

   foo_1.Bar<int, 5>::value[4] = 1;
   foo_2.Bar<char, 7>::value[1] = 'x';

The following is a full compiling example下面是一个完整的编译示例

#include <array>

template <typename T, std::size_t N>
struct Bar
 { std::array<T, N>  value; };

template <typename...>
struct Foo;

template <typename ... Ts, std::size_t ... Ns>
struct Foo<Bar<Ts, Ns>...> : public Bar<Ts, Ns>...
 {
 };

int main ()
 {
   Foo<Bar<int, 5>, Bar<double, 7>> foo_1;
   Foo<Bar<char, 7>> foo_2;

   foo_1.Bar<int, 5>::value[4] = 1;
   foo_2.Bar<char, 7>::value[1] = 'x';
 }

You can pass array type as typename, example in your case will become:您可以将数组类型作为类型名传递,您的示例将变为:

Foo<std::array<int, 5>, std::array<double, 7> > foo_1(
    std::array<int, 5>{0,0,0,0,0},
    std::array<double, 7>{0,0,0,0,0,0,0});

foo_1.Foo<std::array<int, 5>>::value[4] = 1;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM