-
Notifications
You must be signed in to change notification settings - Fork 11.9k
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
[libc++] Use _If for conditional_t #96193
Conversation
@llvm/pr-subscribers-libcxx Author: Nikolas Klauser (philnik777) ChangesThis avoids different instantiations when the if and else types are different, resulting in reduced memory use by the compiler. Full diff: https://github.com/llvm/llvm-project/pull/96193.diff 1 Files Affected:
diff --git a/libcxx/include/__type_traits/conditional.h b/libcxx/include/__type_traits/conditional.h
index 5b5445a837427..7d5849ee824e3 100644
--- a/libcxx/include/__type_traits/conditional.h
+++ b/libcxx/include/__type_traits/conditional.h
@@ -44,15 +44,14 @@ struct _LIBCPP_TEMPLATE_VIS conditional<false, _If, _Then> {
using type _LIBCPP_NODEBUG = _Then;
};
+template <bool _Bp, class _IfRes, class _ElseRes>
+using __conditional_t _LIBCPP_NODEBUG = _If<_Bp, _IfRes, _ElseRes>;
+
#if _LIBCPP_STD_VER >= 14
template <bool _Bp, class _IfRes, class _ElseRes>
-using conditional_t _LIBCPP_NODEBUG = typename conditional<_Bp, _IfRes, _ElseRes>::type;
+using conditional_t _LIBCPP_NODEBUG = __conditional_t<_Bp, _IfRes, _ElseRes>;
#endif
-// Helper so we can use "conditional_t" in all language versions.
-template <bool _Bp, class _If, class _Then>
-using __conditional_t _LIBCPP_NODEBUG = typename conditional<_Bp, _If, _Then>::type;
-
_LIBCPP_END_NAMESPACE_STD
#endif // _LIBCPP___TYPE_TRAITS_CONDITIONAL_H
|
We've run into compile errors after this. I'm not sure if this is an unintentional effect of this change, or if we're doing something wrong. The code looks like this:
And now fails to compile:
We can work around it by using |
I think this patch made us non-conforming. Minimal reproducer: #include <type_traits>
template <class T, class = std::enable_if_t<
!std::is_same_v<int, std::conditional_t<false, T, int>>
>>
void f() { } Godbolt: https://godbolt.org/z/jGxn3nhbz @philnik777 , can you please revert this patch and add a new test case to avoid regressing this? The issue is that the Standard specifies: template< bool B, class T, class F >
using conditional_t = typename conditional<B,T,F>::type; The new implementation does not satisfy this "equivalent to" property because in @zmodem's use case, |
This reverts commit 2274c66. It makes libc++ non-conforming, see discussion on the PR.
Went ahead and reverted this in 3748162, see discussion above. |
We should probably add a test for this. |
This reverts commit 2274c66. It makes libc++ non-conforming, see discussion on the PR.
This avoids different instantiations when the if and else types are different, resulting in reduced memory use by the compiler.
This reverts commit 2274c66. It makes libc++ non-conforming, see discussion on the PR.
This avoids different instantiations when the if and else types are different, resulting in reduced memory use by the compiler.