Skip to content

fix: silent lossy conversions #202

@JohelEGP

Description

@JohelEGP

Consider quantity<dim_length, metre, float>{4518.548561};.
Compiling this generates no error nor warning on GCC with the warnings used by the CI.
If one changes the place where the double is explicitly casted to float from

constexpr quantity(const Value& v) : value_(static_cast<rep>(v)) {}

to

constexpr quantity(const Value& v) : value_(v) {}

then the following warning is emitted.

In file included from /home/johel/Documents/C++/Forks/mpusz/units/src/src/include/units/physical/si/base/time.h:28,
                 from /home/johel/Documents/C++/Forks/mpusz/units/src/src/include/units/chrono.h:26,
                 from quantity_kind_test.cpp:24:
/home/johel/Documents/C++/Forks/mpusz/units/src/src/include/units/quantity.h: In instantiation of ‘constexpr units::quantity<D, U, Rep>::quantity(const Value&) [with Value = double; D = units::physical::si::dim_length; U = units::physical::si::metre; Rep = float]’:
quantity_kind_test.cpp:249:56:   required from here
/home/johel/Documents/C++/Forks/mpusz/units/src/src/include/units/quantity.h:137:48: warning: conversion from ‘double’ to ‘float’ may change value [-Wfloat-conversion]
  137 |   constexpr quantity(const Value& v) : value_(v) {}
      |                                                ^

The user-facing warning is successfully restored. These silent lossy conversions happens at other places, and should be carefully audited.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions