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?
A tuple can have any number of items and they may be of different types (integer, float, list, string, etc.). A tuple can also be created without using parentheses.
std::make_tupleCreates a tuple object, deducing the target type from the types of arguments. For each Ti in Types... , the corresponding type Vi in VTypes... is std::decay<Ti>::type unless application of std::decay results in std::reference_wrapper<X> for some type X , in which case the deduced type is X& .
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>{});
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With