1

How does one construct a tuple with non type template parameters

template <auto... args>
void func()
{
  std::tuple<decltype(args)...> t(args...);
  cout << get<3>(t) << endl;
}

template <auto... args>
struct ZZ
{
  std::tuple<decltype(args)...> t(args...);
};


int main()
{
   func<1,2,3,4>();
   ZZ<1,2,3> z;
}

While it works for func it does not work for the struct and results in the compile error (gcc trunk)

vs.cc:102:35: error: ‘args’ is not a type
  102 |   std::tuple<decltype(args)...> t(args...);
      |                                   ^~~~
1
  • ZZ::t attempts to declare a function. Dec 17, 2020 at 5:20

1 Answer 1

9

The problem is, the default member initializer (since C++11) supports braces and equal-sign initializer, but not parentheses initializer. You can change the code to:

template <auto... args>
struct ZZ
{
  std::tuple<decltype(args)...> t{args...};
  //                             ^       ^
};

Or

template <auto... args>
struct ZZ
{
  std::tuple<decltype(args)...> t = std::tuple<decltype(args)...>(args...);
  //                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
};

And with the help of class template argument deduction (since C++17):

template <auto... args>
struct ZZ
{
  std::tuple<decltype(args)...> t = std::tuple(args...);
  //                              ^^^^^^^^^^^^^^^^^^^^^
};
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.