Add scale-gated unary minus to temperature (enables negative literals)#6
Merged
Conversation
Enables negative absolute-temperature literals such as -123_dc, which previously failed to compile: a user-defined literal never sees the sign, so -123_dc parses as -(123_dc) and temperature had no unary operator-. Unary minus is gated on _is_relative_scale, so it is available on Celsius and Fahrenheit (zero above absolute zero) but a compile error on Kelvin, where a negated temperature would fall below absolute zero. Deltas remain negatable on every scale.
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
Creating a negative absolute-temperature literal, e.g.
-123_dc, failed to compile.In C++ a user-defined literal never receives the sign, so
-123_dcparses as-(123_dc).123_dcproduces adecicelsius(atemperature), andtemperaturedefined++/--/+=/-=but no unaryoperator-— so negating the result was ill-formed. This affected every absolute-temperature literal (_c,_dc,_mc,_f, …); delta literals were unaffected sincedeltaalready has unary minus.Fix
Add a
constexprunaryoperator-totemperature, gated on a new_is_relative_scaletrait (Scale::offset != 0):This is a purely type-level gate with zero runtime cost. It intentionally does not enforce value-level physicality (e.g.
-300_cis still constructible), consistent with the rest of the library, which does not enforce an absolute-zero floor anywhere (kelvin{-5},50_k - 100_k, etc. already compile).Behavior
-123_dcdecicelsius(-123)= −12.3 °C ✅-40_ffahrenheit(-40)✅-25_kTests
New negation section in
thermo_test.cpp:has_unary_minusconcept asserting availability oncelsius/decicelsius/fahrenheitand unavailability onkelvin/decikelvin/millikelvin(the compile-time "it fails" check).static_asserts + runtimeTEST_CASEfor values (-123_dc == -123,-(-5_c) == 5_c, …).Full suite: 157 assertions, 30 test cases, all passing.
Version
Minor bump 2.0.0 → 2.1.0 (backward-compatible feature) across all four version files:
CMakeLists.txt,idf_component.yml,docs/Doxyfile,vcpkg-port/vcpkg.json. The vcpkg port SHA512 is left untouched (set post-release, direct to main).