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...);
| ^~~~
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...);
// ^^^^^^^^^^^^^^^^^^^^^
};
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.