Namespaces
Variants

std::meta::enumerators_of

From cppreference.com
< cpp | meta
 
 
 
Reflection library
 
Reflection types and queries
Type properties
Type property queries
 
Defined in header <meta>
consteval std::vector<std::meta::info> enumerators_of( std::meta::info type_enum );
(since C++26)

Returns a std::vector containing reflections of enumerators of the enumeration type represented by type_enum, in the order in which they are declared.

Parameters

type_enum - a reflection of an enumeration type

Return value

A vector containing the reflections of enumerators, as described above.

Exceptions

Throws std::meta::exception unless std::meta::dealias(type_enum) represents an enumeration type, and std::meta::is_enumerable(type_enum) is true.

Example

#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>();
}

Possible output:

enum class endian {
  little = 1234
  big = 4321
  native = 1234
}
enum class evaluation_semantic {
  ignore = 1
  observe = 2
  enforce = 3
  quick_enforce = 4
}

See also

checks if the reflected type has its list of members completely defined
(function) [edit]
checks if reflection represents an enumerator
(function) [edit]