Better Enums

Reflective compile-time enums for C++

Open-source under the BSD license

Version 0.10.1

To install, just add enum.h to your project.

Visit the GitHub repo for issues, feedback, and the latest development.

Download enum.h GitHub

This page is an advanced demo showing the kind of code you can write on top of Better Enums. It's a valid program — you can download it and try it out. The program runs as part of the automated test suite.

C++17 reflection

Better Enums can be used to approximately implement the enums portion of the C++17 reflection proposal N4428 in C++11. The implementation is approximate in the following senses:

With that out of the way, we can look at a simple example.


The implementation is defined in extra/better-enums/n4428.h. Let's assume that extra/ has been added as a directory to search for include files.

#include <iostream>
#include <enum.h>
#include <better-enums/n4428.h>

Let's declare an enum:

ENUM(Channel, char, Red = 1, Green, Blue)

N4428 proposes three constexpr traits, of which we have two implemented exactly — that is, as constexpr:

constexpr std::size_t   size =
    std::enum_traits<Channel>::enumerators::size;

constexpr Channel       value_0 =
    std::enum_traits<Channel>::enumerators::get<0>::value;
constexpr Channel       value_1 =
    std::enum_traits<Channel>::enumerators::get<1>::value;

Let's check the results:

static_assert(size == 3, "");

static_assert(value_0 == +Channel::Red, "");
static_assert(value_1 == +Channel::Green, "");

Finally, we can try using identifier:

int main()
{
    std::cout
        << std::enum_traits<Channel>::enumerators::get<2>::identifier()
        << std::endl;

    return 0;
}

That prints Blue, as you would expect.