Not a great difference but an alternative to the Julius's answer can the use of the same check (std::bool_constant<(... || std::is_same<T, Ts>{})
or, better, std::disjunction<std::is_same<T, Ts>...>
) be the same things through the declaration of a constexpr
function and a template constexpr
variable
template <typename T, template <typename...> class C, typename ... Ts>
constexpr auto isTypeInList (C<Ts...> const &)
-> std::disjunction<std::is_same<T, Ts>...>;
template <typename T, typename V>
static constexpr bool isTypeInList_v
= decltype(isTypeInList<T>(std::declval<V>()))::value;
and you can use they as follows
using MyVt = std::variant<int, float>;
static_assert( isTypeInList_v<int, MyVt> );
static_assert( isTypeInList_v<double, MyVt> == false );
Not a great improvement but... if you also define (non only declare) the isTypeInList()
function
template <typename T, template <typename...> class C, typename ... Ts>
constexpr auto isTypeInList (C<Ts...> const &)
-> std::disjunction<std::is_same<T, Ts>...>
{ return {}; }
you can also use it directly to check objects
MyVt myVar {0};
static_assert( isTypeInList<int>(myVar) );
avoiding the need of pass through a decltype()
MyVt myVar {0};
static_assert( isTypeInList_v<int, decltype(myVar)> );