[llvm] [ADT] Specialize ValueIsPresent for PointerUnion (PR #121847)

Sergei Barannikov via llvm-commits llvm-commits at lists.llvm.org
Wed Jan 8 14:43:24 PST 2025


================
@@ -614,12 +614,11 @@ template <typename T> struct ValueIsPresent<std::optional<T>> {
   static inline decltype(auto) unwrapValue(std::optional<T> &t) { return *t; }
 };
 
-// If something is "nullable" then we just compare it to nullptr to see if it
-// exists.
+// If something is "nullable" then we just cast it to bool to see if it exists.
 template <typename T>
 struct ValueIsPresent<T, std::enable_if_t<IsNullable<T>>> {
   using UnwrappedType = T;
-  static inline bool isPresent(const T &t) { return t != T(nullptr); }
+  static inline bool isPresent(const T &t) { return static_cast<bool>(t); }
----------------
s-barannikov wrote:

> If we're going down this route, it would probably make sense to also change the enable_if to check std::is_convertible<T, bool> instead?

I think it will make much more sense, will try.

> I think you could also drop the separate std::optional specialization because it also has operator bool.

They are not quite the same, the specialization for `std::optional` derefernces the argument of `unwrapValue()`.
This can probably be guarded by `constexpr if`, but personally I find the separate specialization clearer.


https://github.com/llvm/llvm-project/pull/121847


More information about the llvm-commits mailing list