Variable template template?

Say you have a tuple type and you want to extract its template parameter package to create an instance of another template. If this is a type template, then I might have a utility like this:

template < typename Tuple, template <typename...> typename What > struct PutTupleInT; template < typename... Types, template <typename...> typename What > struct PutTupleInT<std::tuple<Types...>, What> { using Result = What<Types...>; }; 

But what if the desired template is a variable template? While template <typename...> typename What is a placeholder for a type template, what is a placeholder for a variable template?

I tried the following for clang-4.0.0 (the only compiler that now supports non-Pig type template parameters with automatic type), but this failed. Actually, I'm not sure if this is the correct syntax for C ++ 17.

 template < typename Tuple, template <typename...> auto What > struct PutTupleInV; template < typename... Types, template <typename...> auto What > struct PutTupleInV<std::tuple<Types...>, What> { static constexpr auto value = What<Types...>; }; 
+8
c ++ c ++ 14 c ++ 17 template-templates variable-templates
source share
2 answers

I do not think you can do this. Quote from N4606:

ยง14.3.3 [temp.arg.template] / 1

The template argument for the template template must be the class template name or alias template expressed as an ID expression.

The variable template does not meet this requirement.


You can trick a little and use a proxy type to select a template:

 template < typename Tuple, class Proxy> struct PutTupleInTV; template < typename... Types, class Proxy> struct PutTupleInTV<std::tuple<Types...>, Proxy> { static constexpr auto value = Proxy::template value<Types...>; }; 

and then for

 template<typename...> struct foo{}; template<typename... Ts> constexpr foo<Ts...> foo_v{}; struct use_foo { template<typename... Ts> static constexpr auto value = foo_v<Ts...>; }; 

could you say

 PutTupleInTV<tup, use_foo>::value 

live demonstration

+6
source share

PutTupleInTV does not match the name PutTupleInV. You do not specialize in the PutTupleInV template, but use special syntax to create something new called PutTupleInTV.

0
source share

All Articles