Inside the companion
Modern C++ errors
Follow one pixel through validation, std::expected, and a complete-or-unchanged update.
Project files 6
Trace an expected result
Build the pixel experiment and compare successful and rejected updates.
# A complete C++23 expected boundary
A producer validates and encodes a linear channel; a middle layer adds its channel
index to an error; a caller commits a complete pixel only after success. This is
a bounded SDR/gamma-2 teaching example, not exact sRGB or HDR tone mapping.
From the Parallax project root:
```sh
cmake -S native/modern-errors -B native/modern-errors/build/debug -G Ninja -DCMAKE_CXX_COMPILER=clang++ -DCMAKE_BUILD_TYPE=Debug
cmake --build native/modern-errors/build/debug --parallel
ctest --test-dir native/modern-errors/build/debug --output-on-failure --no-tests=error
./native/modern-errors/build/debug/pixel-boundary
```
Expected output:
```text
accepted: 0 128 255
rejected channel 1: channel must be in [0,1]
still: 0 128 255
```
There are two CTest entries, including 17 independently checked API contracts.
Repeat configuration in a separate `build/release` tree with
`-DCMAKE_BUILD_TYPE=Release`, and in `build/sanitize` with
`-DCMAKE_BUILD_TYPE=Debug -DPIXEL_SANITIZE=ON`. Build and test each tree.
The platform must implement C++23 `std::expected` in its standard library.
`cxx_std_23` alone cannot promise that support. The error enum and index do not
allocate. `expected` in general does not remove exceptions or force the caller
to check. Read `*result` only on success and `error()` only on failure.
The demo retains an application exception boundary. The API's commit is a
nonthrowing assignment of three bytes; a more complex owned state needs its own
strong-guarantee publication design.
Suggested independent exercises: use an invalid blue channel, add a non-finite
input at each channel, and explain why the old pixel is never partially updated.
References: [C++23 expected proposal](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p0323r12.html),
[Core Guidelines F.20](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#rf-out),
[Core Guidelines E.6](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#re-raii).
This is the file included with the course. Copy and download give you its complete source.
Taking your work offline? Download all course companions
