-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Improve type traits usage #1413
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Both sides compile even when `if constexpr` is unavailable.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
| template <class _InIt, class _Ty> | ||
| _NODISCARD constexpr bool _Within_limits(_InIt, const _Ty& _Val) { // check whether _Val is within the limits of _Elem | ||
| using _Elem = remove_pointer_t<_InIt>; | ||
| return _Within_limits(_Val, is_signed<_Elem>{}, is_signed<_Ty>{}, bool_constant<-1 == static_cast<_Ty>(-1)>{}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't like this change as much as the others, I think it impacts clarity more. I'm OK with it if the throughput improvement is actually decent though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It was enough to show up on @xiangfan-ms's radar. Clarity will be dramatically improved when we can if constexprize this, hopefully soon.
Fixes MS-internal VSO-1240399 / AB#1240399 reported by @xiangfan-ms.
<random>_Uty(MEOW), which is a functional-syntax C-semantics cast, tostatic_cast<_Uty>(MEOW).is_signedtag dispatch withif _CONSTEXPR_IFis_signed_v. This improves throughput twice (first becauseis_signed_vis silently optimized by MSVC, second becauseif constexpris much cheaper than tag dispatch). Note that even whenif constexpris unavailable, both sides of the branch will compile just fine.<xmemory>is_emptytobool_constantviatypename MEOW::type, wrapis_empty_vinbool_constantbecause MSVC silently optimizesis_empty_v.<xtree>bool_constant<is_same_v<MEOW>>.<xutility>is_signedconverted totrue_typeorfalse_typeduring overload resolution. Again, it's faster to saybool_constant<is_signed_v<MEOW>>. (This logic should be refactored intoif constexprlater, and/or it should use the C++20in_rangetechnique; I am deliberately not pursuing those larger changes now.)