Strongly-typed enums
Use case
Type-safe Mach-O constants that do not pollute namespace.
Explanation
enum class creates type-safe enums. Values are scoped (Type::Value), do not implicitly convert to int and will not clash with other enums.
Code
#include <cstdint>
#include <iostream>
// C enum
// enum OldFileType { Executable = 2, Dylib = 6};
// enum class
// https://en.wikipedia.org/wiki/Mach-O
enum class FileType : uint32_t { Executable = 2, Dylib = 6 };
enum class CPUType : uint32_t { X86_64 = 0x01000007, ARM64 = 0x0100000C };
int main() {
FileType ft = FileType::Executable;
// CPUType cpu = CPUType::ARM64;
// a value of type "FileType" cannot be used to initialize an entity of type "int"
// int y = ft;
// no operator "==" matches these operands
// if (ft == cpu) {}
// no operator "==" matches these operands
// if (ft == 2) {}
uint32_t raw = static_cast<uint32_t>(ft);
std::cout << "FileType raw value: " << raw << "\n";
return 0;
}
Output
$ ./src/c++11/build/strongly-typed-enums
FileType raw value: 2