• Stars
    star
    1,576
  • Rank 28,543 (Top 0.6 %)
  • Language
    C++
  • License
    BSD 2-Clause "Sim...
  • Created almost 9 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

C++ compile-time enum to string, iteration, in a single header file

Better Enums Β  Try online Travis status AppVeyor status

Reflective compile-time enum library with clean syntax, in a single header file, and without dependencies.

Better Enums code overview

In C++11, everything can be used at compile time. You can convert your enums, loop over them, find their max, statically enforce conventions, and pass along the results as template arguments or to constexpr functions. All the reflection is available for your metaprogramming needs.

The interface is the same for C++98 β€” you just have to use most of it at run time only. This library does provide scoped and sized enums, something not built into C++98.

See the project page for full documentation.


Installation

Simply add enum.h to your project.


Additional features

  • Uses only standard C++, though, for C++98, variadic macro support is required (major compilers have it).
  • Supported and tested on clang, gcc, and msvc.
  • Fast compilation. You have to declare a few dozen enums to slow down your compiler as much as only including iostream does.
  • Use any initializers and sparse ranges, just like with a built-in enum.
  • Control over size and alignment β€” you choose the representation type.
  • Stream operators.
  • Does not use the heap and can be compiled with exceptions disabled, for use in minimal freestanding environments.

Limitations

  1. The biggest limitation is that the BETTER_ENUM macro can't be used inside a class. This seems difficult to remove. There is a workaround with typedef (or C++11 using):

    BETTER_ENUM(SomePrefix_Color, uint8_t, Red, Green, Blue)
    
    struct triplet {
        typedef SomePrefix_Color    Color;
        Color                       r, g, b;
    };
    
    triplet::Color  color;

    You can, however, use BETTER_ENUM inside a namespace.

  2. The macro has a soft limit of 64 declared constants. You can extend it by following these instructions. Ultimately, the number of constants is limited by your compiler's maximum macro argument count.

  3. In some cases, it is necessary to prefix constants such as Channel::Red with a + to explicitly promote them to type Channel. For example, if you are doing a comparison:

    channel == +Channel::Red
  4. On msvc, you may need to enable warning C4062 to get switch case exhaustiveness checking.


History

The original version of the library was developed by the author in the winter of 2012-2013 at Hudson River Trading, as a replacement for an older generator called BETTER_ENUM.

More Repositories

1

dream

Tidy, feature-complete Web framework
OCaml
1,514
star
2

lambdasoup

Functional HTML scraping and rewriting with CSS in OCaml
OCaml
371
star
3

promise

Light and type-safe binding to JS promises
Reason
342
star
4

bisect_ppx

Code coverage for OCaml and ReScript
OCaml
297
star
5

luv

Cross-platform asynchronous I/O and system calls
OCaml
275
star
6

markup.ml

Error-recovering streaming HTML5 and XML parsers
OCaml
146
star
7

namespaces

Sane file naming for OCaml projects.
OCaml
71
star
8

hyper

OCaml Web client, composable with Dream [unannounced]
OCaml
63
star
9

dream-serve

Live-reloading server for static sites (eventually also dynamic)
OCaml
51
star
10

repromise_lwt

OCaml
13
star
11

faster-map

A tail-recursive list map with good performance for all list sizes. Not actually written in assembly.
Assembly
13
star
12

reason-native-hello

The smallest possible Reason Native project
Reason
11
star
13

bisect-starter-dune

Bisect_ppx + Dune starter repo
OCaml
6
star
14

bisect-starter-rescript

Bisect_ppx + ReScript starter repo
ReScript
5
star
15

binaries

OCaml binaries for all the platforms
Shell
5
star
16

promise-example-binding

reason-promise binding to node-fetch
Reason
4
star
17

bisect-starter-esy

Bisect_ppx + esy starter repo
OCaml
3
star
18

promise-example-bsb

Hello world using reason-promise
Reason
3
star
19

promise-example-esy

Using native reason-promise with esy
OCaml
3
star
20

bisect-ci-integration-megatest

Bisect_ppx web integrations testing
OCaml
2
star
21

bisect-starter-jsoo

Bisect_ppx + Js_of_ocaml starter repo
OCaml
1
star
22

ocamlformat-binary

Just storage for an Ocamlformat to use in Bisect_ppx's CIs
1
star
23

lwt-manual

OCaml
1
star
24

bisect-starter-jest

Bisect_ppx + Jest starter repo
ReScript
1
star
25

dream-branches

Archived branches from the main Dream repo
OCaml
1
star