Make operators inline friends to improve template matching#2347
Make operators inline friends to improve template matching#2347sethrj merged 11 commits intoceleritas-project:developfrom
Conversation
There was a problem hiding this comment.
Pull request overview
This PR adjusts a number of “strong type” wrappers (e.g., Quantity, OpaqueId, Constant, Array, etc.) to define operators as inline friend functions so they participate in ADL and behave better with implicit conversions (notably std::reference_wrapper), and updates affected call sites/tests accordingly.
Changes:
- Move comparison/arithmetic operators for several core types to inline friend definitions to improve overload resolution/ADL and reduce unrelated template noise in diagnostics.
- Tighten/adjust some comparator behavior (e.g., unsigned-only integer comparisons for
OpaqueId) and update tests/usages to match. - Update documentation tooling (Doxygen predefined macros) to reflect new macro shorthands.
Reviewed changes
Copilot reviewed 19 out of 19 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| test/corecel/random/Selector.test.cc | Update OpaqueId comparisons to avoid signed-integer RHS. |
| test/corecel/OpaqueId.test.cc | Update integer literal to unsigned to match new OpaqueId comparator constraints. |
| test/corecel/math/Quantity.test.cc | Add regression test for std::ref(quantity) operator behavior. |
| test/celeritas/random/ElementSelector.test.cc | Update ElementId comparisons to use typed RHS IDs. |
| src/orange/orangeinp/detail/InfixStringBuilder.hh | Adjust sentinel comparison for surface IDs vs logic tokens. |
| src/orange/orangeinp/detail/BuildLogic.cc | Adjust sentinel comparison for surface IDs vs logic tokens. |
| src/geocel/BoundingBox.hh | Move bbox equality/inequality operators into inline friend definitions. |
| src/corecel/OpaqueId.hh | Redefine OpaqueId comparators/operators as inline friends; restrict integer comparisons. |
| src/corecel/math/Quantity.hh | Add type constraints and move quantity operators/comparators to inline friends. |
| src/corecel/math/detail/QuantityImpl.hh | Add inline friend comparators for UnitlessQuantity. |
| src/corecel/math/Constant.hh | Move constant operators/comparators to inline friend definitions; use new macro shorthands. |
| src/corecel/io/Label.hh | Move label comparators into inline friend definitions. |
| src/corecel/data/PinnedAllocator.hh | Move allocator equality/inequality into inline friend definitions. |
| src/corecel/data/ObserverPtr.hh | Constrain converting ctor; move comparators into inline friend definitions. |
| src/corecel/data/detail/PinnedAllocatorImpl.cc | Remove now-unneeded include after header include changes. |
| src/corecel/cont/Array.hh | Move array equality/inequality into inline friend definitions. |
| src/celeritas/phys/PDGNumber.hh | Move PDGNumber comparators into inline friend definitions. |
| src/celeritas/phys/AtomicNumber.hh | Move AtomicNumber comparators into inline friend definitions. |
| doc/CMakeLists.txt | Update Doxygen predefined macro list for new macro aliases. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## develop #2347 +/- ##
===========================================
+ Coverage 87.18% 87.19% +0.01%
===========================================
Files 1374 1374
Lines 43678 43682 +4
Branches 13840 13836 -4
===========================================
+ Hits 38082 38090 +8
+ Misses 4538 4536 -2
+ Partials 1058 1056 -2
🚀 New features to boost your workflow:
|
Test summary 5 617 files 9 101 suites 19m 21s ⏱️ Results for commit 0451ecd. ♻️ This comment has been updated with latest results. |
5441091 to
4ede50b
Compare
It turns out that many of the operators for Quantity, OpaqueId, etc. have a subtle but unfortunate oversight that, because they're declared as template functions, they don't participate in overload resolution when implicitly casting: this prevents
std::ref(value) + 2from compiling. The fix is to declare them as inline friend functions, where they are reachable by ADL and where the RefWrapper can be automatically looked up via its implicit operator.Moving the rest of the comparison operators into "inline" massively improves output from failed comparisons (e.g., if you try to test equality for a class that isn't convertible or doesn't have an equal sign). Such functions no longer are output in error messages from unrelated classes.
This change also improves the behavior of allowed comparison with OpaqueId: now
MatId{x} < 3is forbidden; onlyMatId{x} < y.size()(since the RHS is an unsigned int) works.