You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This implementation relies on arithmetic conversion, let's see what
happens when we do
std::size_t length{std::strlen(string)};
if (length <= std::numeric_limits<std::int64_t>::max())
return static_cast<std::int64_t>(length);
1) if size_t == uint32_t (or lower), then the comparison operator
invokes integral promotion to uint64_t, the comparison happens, it's
fine.
2) if size_t == uint64_t, then the comparison is done between unsigned
types, which implies a conversion of
std::numeric_limits<std::int64_t>::max() to uint64_t, which happens
without accuracy loss, fine
3) if size_t == uint128_t (or higher), then we invoke integral promotion
of std::int64_t, it's also fine.
So this snippet has the same behavior as the existing one, while being
easier to read.
0 commit comments