2

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.

1
  • 6
    Why do you want to do this? Commented Jul 18, 2016 at 11:39

1 Answer 1

7

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;
 }
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.