Skip to content

C++ prelude: Quantity converts implicitly in both directions, so double→float silently narrows and float+double quantities fail as ambiguous #451

Closed

Description

@matt-edmondson

What's wrong

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 <typename R2>
    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>.

Activity

  1. added
    bug
    on Oct 18, 2026
  2. matt-edmondson commented on Oct 18, 2026

    @matt-edmondson
    ContributorAuthor

    Triage

    • Category: Bug
    • 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.
    • Duplicates / related: No duplicate found.
    • In progress: No open PR references this issue. The open Fold an int.MinValue interval without overflowing [patch] #452 and Resolve dot segments in AbsoluteDirectoryPath.Parent and GetAncestors [patch] #453 cover other topics.

    Generated by Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions