Deriving tuple types

I have a code something like this:

template <typename T> inline typename ::std::enable_if< is_std_tuple<T>{}, T >::type get() { // pull tuple elements from somewhere } 

To deduce the type parameters of the template with which the tuple was created, I did this casting:

 static_cast<T*>(nullptr) 

and pass this as a function parameter

 template <typename ...A> void deduce_tuple(::std::tuple<A...>* const); 

Am I doing UB? Is there a better way?

+5
source share
1 answer

The imperfection here is that we cannot partially specialize function templates. Your path is beautiful, since we are not looking for a null pointer; I would prefer to use the assigned tag:

 template <typename...> struct deduction_tag {}; template <typename... Ts> std::tuple<Ts...> get(deduction_tag<std::tuple<Ts...>>) { // […] } template <typename T> std::enable_if_t<is_std_tuple<T>{}, T> get() { return get(deduction_tag<T>{}); } 
+5
source

Source: https://habr.com/ru/post/1245151/


All Articles