[llvm] [Support] Modernize PointerLikeTypeTraits with llvm::is_detected (NFC) (PR #157021)
Kazu Hirata via llvm-commits
llvm-commits at lists.llvm.org
Thu Sep 4 22:50:45 PDT 2025
https://github.com/kazutakahirata created https://github.com/llvm/llvm-project/pull/157021
This patch consolidates four separate template structs into a single
IsPointerLike struct. A helper alias:
using check_IsPointerLike = ...;
encapsulates the logic to determine a type has pointer-like traits.
>From dfe604bb0570eff63030afb94f5dd0c9dae6247a Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Thu, 4 Sep 2025 21:31:45 -0700
Subject: [PATCH] [Support] Modernize PointerLikeTypeTraits with
llvm::is_detected (NFC)
This patch consolidates four separate template structs into a single
IsPointerLike struct. A helper alias:
using check_IsPointerLike = ...;
encapsulates the logic to determine a type has pointer-like traits.
---
.../llvm/Support/PointerLikeTypeTraits.h | 23 ++++++-------------
1 file changed, 7 insertions(+), 16 deletions(-)
diff --git a/llvm/include/llvm/Support/PointerLikeTypeTraits.h b/llvm/include/llvm/Support/PointerLikeTypeTraits.h
index 1b15f930bd87d..f2e2eef487c61 100644
--- a/llvm/include/llvm/Support/PointerLikeTypeTraits.h
+++ b/llvm/include/llvm/Support/PointerLikeTypeTraits.h
@@ -14,6 +14,7 @@
#ifndef LLVM_SUPPORT_POINTERLIKETYPETRAITS_H
#define LLVM_SUPPORT_POINTERLIKETYPETRAITS_H
+#include "llvm/ADT/STLExtras.h"
#include "llvm/Support/DataTypes.h"
#include <cassert>
#include <type_traits>
@@ -31,25 +32,15 @@ struct ConstantLog2
: std::integral_constant<size_t, ConstantLog2<N / 2>::value + 1> {};
template <> struct ConstantLog2<1> : std::integral_constant<size_t, 0> {};
-// Provide a trait to check if T is pointer-like.
-template <typename T, typename U = void> struct HasPointerLikeTypeTraits {
- static const bool value = false;
-};
-
// sizeof(T) is valid only for a complete T.
template <typename T>
-struct HasPointerLikeTypeTraits<
- T, decltype((sizeof(PointerLikeTypeTraits<T>) + sizeof(T)), void())> {
- static const bool value = true;
-};
+using check_IsPointerLike =
+ std::void_t<decltype(sizeof(PointerLikeTypeTraits<T>)),
+ decltype(sizeof(T))>;
-template <typename T> struct IsPointerLike {
- static const bool value = HasPointerLikeTypeTraits<T>::value;
-};
-
-template <typename T> struct IsPointerLike<T *> {
- static const bool value = true;
-};
+// Provide a trait to check if T is pointer-like.
+template <typename T>
+struct IsPointerLike : is_detected<check_IsPointerLike, T> {};
} // namespace detail
// Provide PointerLikeTypeTraits for non-cvr pointers.
More information about the llvm-commits
mailing list