std :: array作为类的模板参数

时间:2016-07-18 11:36:55

标签: c++ templates c++11

我想知道是否可以使用模板参数设置std::array<std::pair<int,int>>类成员。我不想使用该类的构造函数。

所以它会是这样的:

template<int N, std::array<std::pair<int,int>,N> arr>
class test
{
public:
private:
  std::array<std::pair<int,int>,N> m_arr=arr;
};

int main()
{
  constexpr std::array<std::pair<int,int>,N> arr
  {{
    {1,2},
    {3,4},
    {5,6}
  }};
  test<3,arr> t;
  return 0;
}

提前致谢。

1 个答案:

答案 0 :(得分:1)

如果您在arr之外定义main()并将其作为const引用传递,我认为这是可能的。

以下代码使用我的clang(3.5)

进行编译
#include <array>

constexpr int N {3};

template<int N, const std::array<std::pair<int,int>,N> & arr>
class test
 {
   public:
   private:
      std::array<std::pair<int,int>,N> m_arr = arr;
 };

constexpr std::array<std::pair<int,int>,N> arr
 {{ {1,2}, {3,4}, {5,6} }};

int main()
 {
   test<3,arr> t;
   return 0;
 }
相关问题