nholthaus / units

a compile-time, header-only, dimensional analysis and unit conversion library built on c++14 with no dependencies.
http://nholthaus.github.io/units/
MIT License
934 stars 134 forks source link

Implicit conversion of the time units to/from `std::chrono` #294

Open jinay1991 opened 1 year ago

jinay1991 commented 1 year ago

Feature Request for supporting implicit safe conversion of time units between std::chrono and units::time.

Library used: v2.3.1 Compilers used: GCC v12, Clang v12

nholthaus commented 1 year ago

This is supported in the 3.0 series of releases. Consider backporting to 2.3.

andipeer commented 1 year ago

Parts of this seems to got broken from 3.0.0 Beta to 3.0.0 Beta 2. The following code compiled on Beta, but does not compile on Beta 2 any more:

units::seconds<> s(0);
s += std::chrono::seconds(0);

The problem seems to got introduced with 26242aa0 where an additional overload for operator+= with signature

template<class UnitTypeLhs, class T, std::enable_if_t<traits::is_unit_v<UnitTypeLhs> && !traits::is_unit_v<T>, int> = 0>
constexpr UnitTypeLhs& operator+=(UnitTypeLhs& lhs, T rhs) noexcept;

has been introduced. For std::chrono types on the lhs, this operator now gets precedence and fails in finding the corresponding operator+ used in its implementation.