I want to check if a (long) list of types (in this example just char
and int
) fulfil a (long) list of C++20 concepts (in this example just is_foo
and is_bar
). I currently approach this is as follows:
#include <concepts>
template <class T>
concept is_foo = std::destructible<T>;
template <class T>
concept is_bar = std::integral<T>;
template <class... Ts>
concept all_are_foo = (is_foo<Ts> && ...);
template <class... Ts>
concept all_are_bar = (is_bar<Ts> && ...);
int main() {
static_assert(all_are_foo<char, int>);
static_assert(all_are_bar<char, int>);
return 0;
};
In the above example there are two sources of code duplication:
- Every basic concept
is_x
requires a variadicall_are_x
counterpart. - For each
all_are_x
concept one must re-type the whole list of types.
When the number of concepts and/or the number of types grow this quickly becomes cumbersome. How can I enforce the same concepts in a shorter way? I would like to do something similar to the following pseudocode:
#include <concepts>
#include <tuple>
using type_list = std::tuple<char, int>;
template <class T>
concept is_foo = std::destructible<T>;
template <class T>
concept is_bar = std::integral<T>;
int main() {
static_assert(is_foo<type_list...>); // pseudocode
static_assert(is_bar<type_list...>); // pseudocode
return 0;
};
I’m open to all sorts of solutions. The type_list
doesn’t have to be a std::tuple
, and all template metaprogamming techniques and macros are allowed.
You need to sign in to view this answers