【问题标题】:std::array as template parameter of class [duplicate]std::array 作为类的模板参数[重复]
【发布时间】:2016-07-18 11:36:55
【问题描述】:

我想知道是否可以使用模板参数设置 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;
}

提前致谢。

【问题讨论】:

  • 你为什么要这样做?

标签: c++ templates c++11


【解决方案1】:

如果您在 main() 之外定义 arr 并将其作为 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;
 }

【讨论】:

    猜你喜欢
    • 2021-06-05
    • 1970-01-01
    • 2015-05-19
    • 2013-05-25
    • 2018-08-03
    • 1970-01-01
    • 1970-01-01
    • 2021-03-20
    • 2023-03-10
    相关资源
    最近更新 更多