从可变参数模板创建std :: array [英] Create std::array from variadic template

查看:61
本文介绍了从可变参数模板创建std :: array的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现这样的目标:

#include <string>
#include <array>

enum class MyEnum{
  A,
  B,
  C
};

template<MyEnum... Args>   
class MyClass{
  public:
    MyClass()
    {
    }
  private:
    std::array<MyEnum, sizeof...(Args)> array;   
};

现在我有了一个数组,该数组可以保存所有传递给模板值的值.但是如何用模板参数填充此数组?

Now I have an array, which can hold all passed to template values. But how can I populate this array with template parameters?

推荐答案

如果您想要将所有 MyEnum 值放入 array ,那么您可以将它们解压缩到一个初始化列表中,并使用它初始化 array 通过直接初始化将其初始化:

If what you are wanting is to put all the MyEnum values into array, then you can unpack them into an initialiser list and initialise array with it initialise it with direct initialisation:

MyClass() : array {{ Args... }} { }

但是,您需要一个相当新的编译器才能使用此语法.

You need a fairly new compiler to use this syntax, however.

感谢Xeo纠正了我的答案.

Thanks to Xeo for correcting my answer.

这篇关于从可变参数模板创建std :: array的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆