Skip to content

Conversation

kazutakahirata
Copy link
Contributor

With std::conditional_t, we don't have to have two templates for each
of these type traits.

With std::conditional_t, we don't have to have two templates for each
of these type traits.
@llvmbot
Copy link
Member

llvmbot commented Sep 10, 2025

@llvm/pr-subscribers-llvm-support

Author: Kazu Hirata (kazutakahirata)

Changes

With std::conditional_t, we don't have to have two templates for each
of these type traits.


Full diff: https://github.com/llvm/llvm-project/pull/157791.diff

1 Files Affected:

  • (modified) llvm/include/llvm/Support/type_traits.h (+9-23)
diff --git a/llvm/include/llvm/Support/type_traits.h b/llvm/include/llvm/Support/type_traits.h
index cb7f524447a5e..80713e17a56b9 100644
--- a/llvm/include/llvm/Support/type_traits.h
+++ b/llvm/include/llvm/Support/type_traits.h
@@ -39,35 +39,21 @@ template <typename T> class is_integral_or_enum {
 };
 
 /// If T is a pointer, just return it. If it is not, return T&.
-template <typename T, typename Enable = void>
-struct add_lvalue_reference_if_not_pointer {
-  using type = T &;
-};
-
-template <typename T>
-struct add_lvalue_reference_if_not_pointer<
-    T, std::enable_if_t<std::is_pointer_v<T>>> {
-  using type = T;
+template <typename T> struct add_lvalue_reference_if_not_pointer {
+  using type = std::conditional_t<std::is_pointer_v<T>, T, T &>;
 };
 
 /// If T is a pointer to X, return a pointer to const X. If it is not,
 /// return const T.
-template <typename T, typename Enable = void> struct add_const_past_pointer {
-  using type = const T;
+template <typename T> struct add_const_past_pointer {
+  using type = std::conditional_t<std::is_pointer_v<T>,
+                                  const std::remove_pointer_t<T> *, const T>;
 };
 
-template <typename T>
-struct add_const_past_pointer<T, std::enable_if_t<std::is_pointer_v<T>>> {
-  using type = const std::remove_pointer_t<T> *;
-};
-
-template <typename T, typename Enable = void>
-struct const_pointer_or_const_ref {
-  using type = const T &;
-};
-template <typename T>
-struct const_pointer_or_const_ref<T, std::enable_if_t<std::is_pointer_v<T>>> {
-  using type = typename add_const_past_pointer<T>::type;
+template <typename T> struct const_pointer_or_const_ref {
+  using type =
+      std::conditional_t<std::is_pointer_v<T>,
+                         typename add_const_past_pointer<T>::type, const T &>;
 };
 
 namespace detail {

@kazutakahirata kazutakahirata merged commit 65994cc into llvm:main Sep 10, 2025
11 checks passed
@kazutakahirata kazutakahirata deleted the cleanup_20250909_Support_type_traits_std_conditional_t branch September 10, 2025 15:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants