std::meta::enumerators_of
Определён в заголовочном файле <meta> | ||
|---|---|---|
consteval std::vector<std::meta::info> enumerators_of( std::meta::info type_enum ); | (начиная с C++26) |
Возвращает std::vector, содержащий отражения перечислителей типа перечисления, представленного type_enum, в порядке их объявления.
Параметры
| type_enum | - | отражение типа перечисления |
Возвращаемое значение
Вектор, содержащий отражения перечислителей, как описано выше.
Исключения
Выбрасывает std::meta::exception, если std::meta::dealias(type_enum) не представляет тип перечисления или std::meta::is_enumerable(type_enum) не является true.
Пример
#include <bit>
#include <contracts>
#include <iostream>
#include <meta>
#include <utility>
template <typename E>
requires(std::meta::is_enum_type(^^E))
constexpr void print_enum()
{
constexpr static auto enum_name = std::meta::identifier_of(^^E);
constexpr static bool scoped = std::meta::is_scoped_enum_type(^^E);
std::cout << "enum " << (scoped ? "class " : " ") << enum_name << " {\n";
// store the list of enumerators' reflections into a static storage
constexpr static auto enumerators = std::define_static_array(enumerators_of(^^E));
template for (constexpr auto enumerator : enumerators)
{
constexpr auto name = std::meta::identifier_of(enumerator);
// splice the enumerator reflection back into a usable runtime value
constexpr E value = [: enumerator :];
std::cout << " " << name << " = " << std::to_underlying(value) << '\n';
}
std::cout << "}\n";
}
int main()
{
print_enum<std::endian>();
print_enum<std::contracts::evaluation_semantic>();
}
Возможный вывод:
enum class endian {
little = 1234
big = 4321
native = 1234
}
enum class evaluation_semantic {
ignore = 1
observe = 2
enforce = 3
quick_enforce = 4
}
См. также
|
(C++26) | проверяет, полностью ли определён список членов отражённого типа (функция) |
|
(C++26) | проверяет, представляет ли отражение перечислитель (функция) |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/cpp/meta/enumerators_of