Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/137.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ std::数组作为类的模板参数_C++_Templates_C++11 - Fatal编程技术网

C++ std::数组作为类的模板参数

C++ std::数组作为类的模板参数,c++,templates,c++11,C++,Templates,C++11,我想知道是否可以使用模板参数设置std::array类成员。我不想使用类的构造函数 所以应该是这样的: 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::arra

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

所以应该是这样的:

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;
}
模板
课堂测试
{
公众:
私人:
std::数组m_arr=arr;
};
int main()
{
constexpr std::数组arr
{{
{1,2},
{3,4},
{5,6}
}};
试验t;
返回0;
}

提前感谢。

如果您在
main()
外部定义
arr
,并将其作为常量引用传递,我想这是可能的

以下代码使用my clang(3.5)编译

#包括
constexpr int N{3};
模板
课堂测试
{
公众:
私人:
std::数组m_arr=arr;
};
constexpr std::数组arr
{{ {1,2}, {3,4}, {5,6} }};
int main()
{
试验t;
返回0;
}

为什么要这样做?
#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;
 }