You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The initial motivation for this proposal is to offer an exception-free error handling mechanism better aligned with functional safety principles. Although MISRA C++:2023 no longer outright bans exceptions, it demands a strong justification for their use—an expectation that certification bodies may continue to scrutinize.
Feedback from zserio users working on safety-critical systems underscores the need for an alternative approach. This proposal is not intended to be definitive but should serve as a starting point for broader discussion.
Drawing from C++23’s std::expected and Rust’s robust error-handling paradigm, the proposed approach aims to balance clarity, maintainability, and the stringent demands of safety standards. While I have not yet fully explored all implications of this design, it highlights important directions worth discussing. I welcome feedback on this approach as well as alternative suggestions.
Context
The zserio C++ codebase (both C++11 and C++17) currently uses exceptions extensively,
which poses challenges for functional safety applications where exceptions are typically prohibited.
Proposed Solution: tl::expected<T,E>
I'd like to propose adopting tl::expected - a C++17 backport of C++23's std::expected. It's essentially C++'s version of Rust's Result<T,E>:
// Instead of:uint32_treadBits(uint8_t numBits); // throws on error// We'd have:
tl::expected<uint32_t, ErrorInfo> tryReadBits(uint8_t numBits) noexcept;
// Usage:auto result = reader.tryReadBits(8);
if (!result.has_value()) {
// Handle error explicitlyreturntl::make_unexpected(result.error());
}
uint32_t value = result.value();
Why This Approach?
Safety Certification Ready: When compiled with -fno-exceptions, tl::expected becomes deterministic with no hidden control flow
Zero Overhead: Stores values/errors in-place (union) - no heap allocation
Modern Ergonomics: Supports monadic operations (.map(), .and_then())
Future-Proof: Easy migration to std::expected in C++23
Key Challenge: Constructors
Since constructors can't return expected, we'd use factory methods:
What's your experience with std::expected or similar types?
Any concerns about the factory method pattern for object construction?
Should we have a compatibility transition phase for easier migration?
Looking forward to your thoughts! This is a significant change, but I believe it's necessary for zserio to meet modern safety standards while maintaining good ergonomics.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
Motivation
The initial motivation for this proposal is to offer an exception-free error handling mechanism better aligned with functional safety principles. Although MISRA C++:2023 no longer outright bans exceptions, it demands a strong justification for their use—an expectation that certification bodies may continue to scrutinize.
Feedback from zserio users working on safety-critical systems underscores the need for an alternative approach. This proposal is not intended to be definitive but should serve as a starting point for broader discussion.
Drawing from C++23’s std::expected and Rust’s robust error-handling paradigm, the proposed approach aims to balance clarity, maintainability, and the stringent demands of safety standards. While I have not yet fully explored all implications of this design, it highlights important directions worth discussing. I welcome feedback on this approach as well as alternative suggestions.
Context
The zserio C++ codebase (both C++11 and C++17) currently uses exceptions extensively,
which poses challenges for functional safety applications where exceptions are typically prohibited.
Proposed Solution:
tl::expected<T,E>I'd like to propose adopting
tl::expected- a C++17 backport of C++23'sstd::expected. It's essentially C++'s version of Rust'sResult<T,E>:Why This Approach?
-fno-exceptions,tl::expectedbecomes deterministic with no hidden control flow.map(),.and_then())std::expectedin C++23Key Challenge: Constructors
Since constructors can't return
expected, we'd use factory methods:Points for Discussion
API Verbosity: Error handling becomes more explicit. Is this acceptable?
Breaking Changes: All APIs would eventually change. How do we minimize user impact?
Error Types: Should we use error codes or richer error types?
What I'd Like to Know
std::expectedor similar types?Looking forward to your thoughts! This is a significant change, but I believe it's necessary for zserio to meet modern safety standards while maintaining good ergonomics.
References
Beta Was this translation helpful? Give feedback.
All reactions