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
{{ message }}
Repository navigation
C++ prelude: Quantity converts implicitly in both directions, so double→float silently narrows and float+double quantities fail as ambiguous #451
The shipped prelude documents its converting constructor as widening-only (quantity.hpp#L45-L51):
// Widening the representation of the same dimension is safe and implicit.template <typenameR2>
requires(!std::is_same_v<R2, Rep> && std::is_convertible_v<R2, Rep>)
constexpr Quantity(Quantity<D, R2> other) noexcept
: value_(static_cast<Rep>(other.count()))
The constraint uses std::is_convertible_v<R2, Rep>, which is also true for double → float, double → int and float → int. Every representation therefore converts implicitly to every other. The conversion goes through static_cast, so -Wconversion does not warn.
Failure scenarios (g++ and clang++, -std=c++20 -Wall -Wextra -Wconversion, no warnings)
using L = q::Dimension<1>;
q::Quantity<L, float> f = q::Quantity<L, double>{1e40}; // compiles; f.count() == inf
q::Quantity<L, float> f2 = q::Quantity<L, double>{0.1}; // 0.10000000149011612
q::Quantity<L, int> i = q::Quantity<L, double>{2.9}; // 2, truncated
The conversion runs both ways, so mixing representations does not compile:
q::Quantity<L, float> f{1.f}; q::Quantity<L, double> d{2.0};
auto s = f + d;
// error: ambiguous overload for 'operator+' (operand types are// 'q::Quantity<q::Dimension<1>, float>' and 'q::Quantity<q::Dimension<1>, double>')
If the conversion only widened, f + d would resolve to the double hidden friend, as the comment describes.
Generated code pins Rep to the default float, so it does not hit this today. The prelude is public API, though, and L27 invites callers to use a different representation ("so a component can use a narrower representation"). Doing that today either loses precision silently or fails to compile.
Suggested fix
Constrain the implicit conversion to non-narrowing cases, e.g. requires(!std::is_same_v<R2, Rep> && requires(R2 r) { Rep{ r }; }). List-initialisation rejects narrowing.
Optionally, add an explicit converting constructor for the narrowing direction.
Acceptance criteria (compile tests in the style of Semantics.Cpp.Test/GeneratedCppCompilesTests.cs)
Quantity<D,float> f = Quantity<D,double>{...}; is refused.
Quantity<D,double> d = Quantity<D,float>{...}; compiles.
Quantity<D,float>{} + Quantity<D,double>{} compiles and yields Quantity<D,double>.
Priority: Medium. Generated code pins Rep = float, so it is unaffected today. The prelude is public API, though, and it invites alternative representations. For those callers it either narrows silently or fails to compile with an ambiguous overload. The fix is a one-line constraint change (non-narrowing list-init check) plus compile tests.
Area / suggested assignment: Semantics.Cpp prelude (Semantics.Cpp/Prelude/quantity.hpp). Tests go in Semantics.Cpp.Test.
Activity
matt-edmondson commentedon Oct 18, 2026
Triage
Rep = float, so it is unaffected today. The prelude is public API, though, and it invites alternative representations. For those callers it either narrows silently or fails to compile with an ambiguous overload. The fix is a one-line constraint change (non-narrowing list-init check) plus compile tests.Semantics.Cpp/Prelude/quantity.hpp). Tests go inSemantics.Cpp.Test.Generated by Claude Code
[patch] C++ prelude: a representation widens implicitly and narrows o…