I want to define a concept in C++ (<= C++20) to check if a type matches any of the types define in a type-list struct.
The following is my attempt so far:
template<typename... Types>
struct TypeList {};
using SupportedTypes = TypeList<int, bool, float, long>;
template<typename T, typename... Types>
concept IsAnyOf = (std::is_same_v<T, Types> || ...);
static_assert(IsAnyOf<bool, SupportedTypes>);
I have also tried using:
template<typename T, typename... Types>
concept IsAnyOf = std::disjunction_v<std::is_same<T, Types>...>;
But my static assertion fails:
Static assertion failed
because 'IsSupportedType<_Bool, SupportedTypes>' evaluated to false
because 'std::is_same_v<_Bool, meta::TypeList<int, _Bool, float, long> >' evaluated to false
I understand it probably has to do with the fact that I'm passing SupportedTypes
to the concept without properly unpacking the types inside it, and hence in the static assertion I'm checking if bool is the same as SupportedTypes
, as opposed to checking if bool is the same as any of the types inside SupportedTypes
; but I can't get it to work nonetheless.