Date: Sat, 6 Sep 2025 11:05:58 +0000
Problem
The current ifstream/ofstream API allows illogical mode combinations:
std::ifstream f("data.txt", std::ios::out); // Compiles, but doesn't work
Modes are bitmasks, error-prone, and not checked at compile time.
The current std::ios::openmode is an unscoped bitmask with limited type safety and poor readability.
Proposal
This proposal introduces a strongly typed alternative using enum class Mode, enabling clearer semantics, safer combinations, and future extensibility.
auto f = open_file<Mode::read>("data.txt");
auto g = open_file<write | Mode ::binary>("log.bin");
Mode
The mode is now explicit and scoped, reducing ambiguity and improving code clarity. Bitwise combinations are supported via overloaded operators, and modes can be introspected or validated at compile-time.
The template parameter Mode uses an enum class for clarity and safety. :
enum class Mode : std::uint8_t {
read = 0x01,
write = 0x02,
binary = 0x04,
append = 0x08
};
Benefits
* Compile-time safety
* Clear intent
* Modern C++ style
* No breaking changes — this is a parallel API
The current ifstream/ofstream API allows illogical mode combinations:
std::ifstream f("data.txt", std::ios::out); // Compiles, but doesn't work
Modes are bitmasks, error-prone, and not checked at compile time.
The current std::ios::openmode is an unscoped bitmask with limited type safety and poor readability.
Proposal
This proposal introduces a strongly typed alternative using enum class Mode, enabling clearer semantics, safer combinations, and future extensibility.
auto f = open_file<Mode::read>("data.txt");
auto g = open_file<write | Mode ::binary>("log.bin");
Mode
The mode is now explicit and scoped, reducing ambiguity and improving code clarity. Bitwise combinations are supported via overloaded operators, and modes can be introspected or validated at compile-time.
The template parameter Mode uses an enum class for clarity and safety. :
enum class Mode : std::uint8_t {
read = 0x01,
write = 0x02,
binary = 0x04,
append = 0x08
};
Benefits
* Compile-time safety
* Clear intent
* Modern C++ style
* No breaking changes — this is a parallel API
Received on 2025-09-06 11:06:06