Make std::formatter usable with exceptions disabled - #7
Merged
Conversation
The formatter specializations' parse() reported an unusable format spec (a precision with no SI prefix and an empty spec) with a bare throw, which is a hard compile error under -fno-exceptions for any use of the formatters — even valid ones — making them unusable on embedded targets such as ESP-IDF with CONFIG_COMPILER_CXX_EXCEPTIONS unset. The error branch is now discarded via `if constexpr` when the precision has an SI prefix, and otherwise reports through a _format_error() helper that throws std::format_error when exceptions are available and calls std::abort() when they are not. Because the helper is not constexpr, constant evaluation of the format string still rejects a bad spec at compile time in both modes. Adds a framework-free test target built with -fno-exceptions (Catch2 requires exceptions, so it cannot live in thermo_tests).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The
std::formatterspecializations'parse()reported an unusable format spec (a precision with no SI prefix, formatted with an empty spec) with a barethrow format_error(...). Under-fno-exceptions— the default on embedded targets such as ESP-IDF withCONFIG_COMPILER_CXX_EXCEPTIONSunset — athrowin any instantiated function is a hard compile error, so even a perfectly validstd::format("{}", celsius(20))failed to compile.Fix
if constexprwhen the precision has an SI prefix, so it is never instantiated for formatters that cannot need it._format_error()helper: it throwsstd::format_errorwhen exceptions are available, and callsstd::abort()when they are not. Because the helper is not constexpr, consteval format-string checking still rejects a bad spec at compile time in both modes; the abort is only reachable through a runtime format string (std::vformat) with exceptions disabled, where there is no way to report the error.Tests
tests/no_exceptions_test.cpp: a framework-free target compiled with-fno-exceptionsexercising both formatters with empty and explicit specs (Catch2 requires exceptions, so it cannot live inthermo_tests). Skipped on MSVC, which spells the option differently and declares__cpp_exceptionsregardless.Bumps version to 2.1.1 (CMakeLists, idf_component.yml, docs/Doxyfile, vcpkg-port/vcpkg.json).