Код: Выделить всё
template
concept ConvertibleToInt = std::convertible_to;
template
concept AllAlternativesConvertibleToInt = [](std::index_sequence){
return (ConvertibleToInt && ...);
}(std::make_index_sequence{});
Код: Выделить всё
static_assert(AllAlternativesConvertibleToInt);
Код: Выделить всё
:12:15: error: static assertion failed
12 | static_assert(AllAlternativesConvertibleToInt);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
:12:15: note: because 'std::variant' (aka 'variant') does not satisfy 'AllAlternativesConvertibleToInt'
:8:43: note: because '[](std::index_sequence) {
return (ConvertibleToInt && ...);
}(std::make_index_sequence{})' evaluated to false
8 | concept AllAlternativesConvertibleToInt = [](std::index_sequence){
| ^
1 error generated.
Мне не удалось найти способ иметь пакет параметров от 0 до std::variant_size_v< /code>, кроме написания вручную следующим образом:
Код: Выделить всё
template
concept ConvertibleToInt = std::convertible_to;
template
concept AllAlternativesConvertibleToIntImpl = (ConvertibleToInt && ...);
template
concept AllAlternativesConvertibleToInt = AllAlternativesConvertibleToIntImpl;
static_assert(AllAlternativesConvertibleToInt);
Код: Выделить всё
:24:15: error: static assertion failed
24 | static_assert(AllAlternativesConvertibleToInt);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
:24:15: note: because 'std::variant' (aka 'variant') does not satisfy 'AllAlternativesConvertibleToInt'
:22:43: note: because 'AllAlternativesConvertibleToIntImpl' evaluated to false
22 | concept AllAlternativesConvertibleToInt = AllAlternativesConvertibleToIntImpl;
| ^
:19:48: note: because 'std::variant_alternative_t' (aka 'std::nullptr_t') does not satisfy 'ConvertibleToInt'
19 | concept AllAlternativesConvertibleToIntImpl = (ConvertibleToInt && ...);
| ^
:16:28: note: because 'std::convertible_to' evaluated to false
16 | concept ConvertibleToInt = std::convertible_to;
| ^
/opt/compiler-explorer/gcc-14.2.0/lib/gcc/x86_64-linux-gnu/14.2.0/../../../../include/c++/14.2.0/concepts:79:30: note: because 'is_convertible_v' evaluated to false
79 | concept convertible_to = is_convertible_v
| ^
1 error generated.
Подробнее здесь: https://stackoverflow.com/questions/791 ... -a-concept
Мобильная версия