C++ Basics: Define Explicit Operators for Scoped Flags
Combine named flags while keeping the bitwise conversion inside a small typed interface.
Prepare a scratch directory
Tested on Linux x86_64 with GCC 14.2.0. Install a C++20-capable compiler and standard-library development headers, with g++ on PATH. You also need Bash, a text editor, and the mktemp, rm, and rmdir utilities. No third-party library is required.
Keep the same terminal open. Stop if setup fails, before saving the source:
start_directory="$PWD"
work_directory="$(mktemp -d)" || exit 1
cd -- "$work_directory" || exit 1Run the example
Save this as enum-class-bitmask-operators-demo.cpp:
#include <iostream>
#include <type_traits>
enum class Permission : unsigned int {
none = 0,
read = 1U << 0,
write = 1U << 1,
execute = 1U << 2
};
constexpr Permission operator|(Permission left, Permission right)
{
using Storage = std::underlying_type_t<Permission>;
return static_cast<Permission>(static_cast<Storage>(left) | static_cast<Storage>(right));
}
constexpr bool contains_all(Permission available, Permission requested)
{
using Storage = std::underlying_type_t<Permission>;
const auto available_bits = static_cast<Storage>(available);
const auto requested_bits = static_cast<Storage>(requested);
return (available_bits & requested_bits) == requested_bits;
}
int main()
{
const auto permissions = Permission::read | Permission::write;
std::cout << std::boolalpha;
std::cout << "Can read: " << contains_all(permissions, Permission::read) << '\n';
std::cout << "Can execute: " << contains_all(permissions, Permission::execute) << '\n';
std::cout << "Has read and write: "
<< contains_all(permissions, Permission::read | Permission::write) << '\n';
}Compile and run it. The program runs only after compilation succeeds:
g++ -std=c++20 -Wall -Wextra -Werror -pedantic enum-class-bitmask-operators-demo.cpp -o enum-class-bitmask-operators-demo &&
./enum-class-bitmask-operators-demoObserved output:
Can read: true
Can execute: false
Has read and write: trueRead the result
The custom operator returns another Permission rather than exposing a raw integer. contains_all checks every requested bit, including a combined read-and-write mask. An execute request fails because that bit was never set.
Apply it carefully
Scoped enumerations do not acquire bitwise operators automatically. Define only the operations your interface needs. The fixed unsigned underlying type and small unsigned shifts keep these constants representable. Arbitrary external integers still need validation against the known-bit mask; casting them does not validate permissions. Requesting none succeeds because an empty set of required bits is satisfied.
The C++ working draft describes the relevant rules. This example uses C++20 facilities; the current working draft also documents later additions.
Clean up
Remove only the source and executable created here, then return to the starting directory:
rm -- enum-class-bitmask-operators-demo.cpp enum-class-bitmask-operators-demo
cd -- "$start_directory" || exit 1
rmdir -- "$work_directory"
unset start_directory work_directoryIf the directory is not empty, inspect its remaining contents before removing anything else.
Acknowledgements
This post was created with assistance from AI.