Skip to content

Commit 7b199f1

Browse files
committed
make Number able to be converted to double implicitly
1 parent dbe34e3 commit 7b199f1

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

include/units/units.hpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ template <isQuantity Q, isQuantity R> constexpr bool operator>(const Q& lhs, con
261261
return (lhs.internal() > rhs.internal());
262262
}
263263

264-
#define NEW_UNIT(Name, suffix, m, l, t, i, a, o, j, n) \
264+
#define NEW_UNIT_FULL(Name, suffix, m, l, t, i, a, o, j, n, extra) \
265265
class Name : public Quantity<std::ratio<m>, std::ratio<l>, std::ratio<t>, std::ratio<i>, std::ratio<a>, \
266266
std::ratio<o>, std::ratio<j>, std::ratio<n>> { \
267267
public: \
@@ -273,6 +273,7 @@ template <isQuantity Q, isQuantity R> constexpr bool operator>(const Q& lhs, con
273273
value) \
274274
: Quantity<std::ratio<m>, std::ratio<l>, std::ratio<t>, std::ratio<i>, std::ratio<a>, std::ratio<o>, \
275275
std::ratio<j>, std::ratio<n>>(value) {}; \
276+
extra \
276277
}; \
277278
template <> struct LookupName<Quantity<std::ratio<m>, std::ratio<l>, std::ratio<t>, std::ratio<i>, std::ratio<a>, \
278279
std::ratio<o>, std::ratio<j>, std::ratio<n>>> { \
@@ -294,6 +295,8 @@ template <isQuantity Q, isQuantity R> constexpr bool operator>(const Q& lhs, con
294295
constexpr inline Name from_##suffix(double value) { return Name(value); } \
295296
constexpr inline double to_##suffix(Name quantity) { return quantity.internal(); }
296297

298+
#define NEW_UNIT(Name, suffix, m, l, t, i, a, o, j, n) NEW_UNIT_FULL(Name, suffix, m, l, t, i, a, o, j, n, )
299+
297300
#define NEW_UNIT_LITERAL(Name, suffix, multiple) \
298301
[[maybe_unused]] constexpr Name suffix = multiple; \
299302
constexpr Name operator""_##suffix(long double value) { return static_cast<double>(value) * multiple; } \
@@ -312,7 +315,7 @@ template <isQuantity Q, isQuantity R> constexpr bool operator>(const Q& lhs, con
312315
NEW_UNIT_LITERAL(Name, u##base, base / 1E6) \
313316
NEW_UNIT_LITERAL(Name, n##base, base / 1E9)
314317

315-
NEW_UNIT(Number, num, 0, 0, 0, 0, 0, 0, 0, 0)
318+
NEW_UNIT_FULL(Number, num, 0, 0, 0, 0, 0, 0, 0, 0, constexpr operator double() { return this->value; })
316319
NEW_UNIT_LITERAL(Number, percent, num / 100.0);
317320

318321
NEW_UNIT(Mass, kg, 1, 0, 0, 0, 0, 0, 0, 0)

0 commit comments

Comments
 (0)