Skip to content

Commit 550edc1

Browse files
committed
hide number operator overloads in a namespace
1 parent 8902c87 commit 550edc1

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

include/units/units.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,31 +363,38 @@ constexpr inline double to_num(Number quantity) { return quantity.internal(); }
363363
constexpr bool operator op(Number lhs, double rhs) { return (lhs.internal() op rhs); } \
364364
constexpr bool operator op(double lhs, Number rhs) { return (lhs op rhs.internal()); }
365365

366+
namespace units_double_ops {
366367
NEW_NUM_TO_DOUBLE_COMPARISON(==)
367368
NEW_NUM_TO_DOUBLE_COMPARISON(!=)
368369
NEW_NUM_TO_DOUBLE_COMPARISON(<=)
369370
NEW_NUM_TO_DOUBLE_COMPARISON(>=)
370371
NEW_NUM_TO_DOUBLE_COMPARISON(<)
371372
NEW_NUM_TO_DOUBLE_COMPARISON(>)
373+
} // namespace units_double_ops
372374

373375
#define NEW_NUM_AND_DOUBLE_OPERATION(op) \
374376
constexpr Number operator op(Number lhs, double rhs) { return (lhs.internal() op rhs); } \
375377
constexpr Number operator op(double lhs, Number rhs) { return (lhs op rhs.internal()); }
376378

379+
namespace units_double_ops {
377380
NEW_NUM_AND_DOUBLE_OPERATION(+)
378381
NEW_NUM_AND_DOUBLE_OPERATION(-)
379382
NEW_NUM_AND_DOUBLE_OPERATION(*)
380383
NEW_NUM_AND_DOUBLE_OPERATION(/)
384+
} // namespace units_double_ops
381385

382386
#define NEW_NUM_AND_DOUBLE_ASSIGNMENT(op) \
383387
constexpr void operator op##=(Number& lhs, double rhs) { lhs = lhs.internal() op rhs; } \
384388
constexpr void operator op##=(double& lhs, Number rhs) { lhs = lhs op rhs.internal(); }
385389

390+
namespace units_double_ops {
386391
NEW_NUM_AND_DOUBLE_ASSIGNMENT(+)
387392
NEW_NUM_AND_DOUBLE_ASSIGNMENT(-)
388393
NEW_NUM_AND_DOUBLE_ASSIGNMENT(*)
389394
NEW_NUM_AND_DOUBLE_ASSIGNMENT(/)
395+
} // namespace units_double_ops
390396

397+
namespace units_double_ops {
391398
constexpr Number& operator++(Number& lhs, int) {
392399
lhs += 1;
393400
return lhs;
@@ -409,6 +416,7 @@ constexpr Number operator--(Number& lhs) {
409416
lhs -= 1;
410417
return copy;
411418
}
419+
} // namespace units_double_ops
412420

413421
NEW_UNIT_LITERAL(Number, percent, num / 100)
414422

src/main.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ constexpr void angleRangeTests() {
6060
}
6161

6262
constexpr Number numAssignmentTests() {
63+
using namespace units_double_ops;
6364
Number n = 1_num; // 1
6465
n += 2; // 3
6566
n--; // 2
@@ -71,6 +72,7 @@ constexpr Number numAssignmentTests() {
7172
}
7273

7374
constexpr double doubleAssignmentTests() {
75+
using namespace units_double_ops;
7476
double d = 1; // 1
7577
d += 2_num; // 3
7678
d -= 2_num; // 1
@@ -80,6 +82,7 @@ constexpr double doubleAssignmentTests() {
8082
}
8183

8284
void numberOperatorTests() {
85+
using namespace units_double_ops;
8386
static_assert(1_num + 2 == 3);
8487
static_assert(1 + 2_num <= 3);
8588
static_assert(1 / 2_num >= 0);

0 commit comments

Comments
 (0)