1

I am wondering if a std::array<std::pair<int,int>> class member can be set using a template parameter. I do not want to use the constructor of the class.

So it would be something like this:

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;
}

Thanks in advance.

Holt
  • 36,600
  • 7
  • 92
  • 139
Mayhem
  • 487
  • 2
  • 5
  • 13

1 Answers1

4

If you define arr outside main() and you pass it as a const reference, I suppose it's possible.

The following code compile with my 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;
 }
max66
  • 65,235
  • 10
  • 71
  • 111