简体   繁体   中英

deducing a tuple's types

I have code something like this:

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

In order to deduce the template type parameters the tuple was instantiated with, I did this casting:

static_cast<T*>(nullptr)

and pass this as a parameter to a function

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

Am I committing UB? Is there a better way?

The imperfection here is that we cannot partially specialize function templates. Your way is fine, since we're not dereferencing the null pointer; I'd prefer using a designated 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>{});
}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM