Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Direct-list-initialization of enums

Use case

Initialize scoped enum from raw value (without cast).

Explanation

Scoped enums can now be initialized with braces from their underlying type.

Code

#include <cstdint>
#include <iostream>

// https://en.wikipedia.org/wiki/Mach-O
enum class MHFileType : uint32_t {};

int main() {
  MHFileType exe{0x2};

  std::cout << "Filetype: 0x" << std::hex << static_cast<uint32_t>(exe) << "\n";

  return 0;
}

View on GitHub.

Output

$ ./src/c++17/build/direct-list-initialization-of-enums
Filetype: 0x2