Skip to content

Support SFINAE in Constant representability check#669

Merged
chiphogg merged 4 commits into
mainfrom
chiphogg/sfinae-friendly-constant-check
Apr 6, 2026
Merged

Support SFINAE in Constant representability check#669
chiphogg merged 4 commits into
mainfrom
chiphogg/sfinae-friendly-constant-check

Conversation

@chiphogg
Copy link
Copy Markdown
Member

@chiphogg chiphogg commented Apr 4, 2026

Right now, we have an unconditional UnitRatio invocation, which
produces a hard compiler error. We thought this was fine, because
nobody should be invoking this check with different-dimension units.
However, it somehow does get invoked in Aurora-internal code.

I tried reproducing the error in the Au repo only, and couldn't do it.
But in any case, we can fix it by just avoiding the hard error.

The most straightforward way to fix it was to just make a new trait,
IsUnitRatioRepresentableIn<T, U1, U2>. This doesn't seem like
something I want to expose to end users, so I kept it as a library
internal detail.

chiphogg added 2 commits April 4, 2026 13:37
Right now, we have an unconditional `UnitRatio` invocation, which
produces a hard compiler error.  We thought this was fine, because
nobody should be invoking this check with different-dimension units.
However, it somehow does get invoked in Aurora-internal code.

I tried reproducing the error in the Au repo only, and couldn't do it.
But in any case, we can fix it by just avoiding the hard error.

The most straightforward way to fix it was to just make a new trait,
`IsUnitRatioRepresentableIn<T, U1, U2>`.  This doesn't seem like
something I want to expose to end users, so I kept it as a library
internal detail.
Comment thread au/constant_test.cc
}

TEST(CanStoreValueIn, ReturnsFalseRatherThanHardErrorForDifferentDimensions) {
constexpr bool result = Constant<Meters>::can_store_value_in<long>(Nano<Seconds>{});
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the size of long matter? It's 8 bytes for GCC and 4 bytes for MSVC: godbolt.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this specific case, definitely not. The answer doesn't depend on the ratio, because the ratio is not even well defined. It will return false because the dimensions are different.

Comment thread au/unit_of_measure.hh
// same dimension.
namespace detail {
template <typename T, typename U1, typename U2>
struct IsUnitRatioRepresentableIn;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Q: Does it make sense to unit test this?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hadn't been planning on it initially, but I agree it makes sense to add them.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

@chiphogg chiphogg merged commit 860ae5f into main Apr 6, 2026
27 checks passed
@chiphogg chiphogg deleted the chiphogg/sfinae-friendly-constant-check branch April 6, 2026 14:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants