[libcxx-commits] [libcxx] [libc++] Implement LWG3545: std::pointer_traits should be SFINAE-friendly. (PR #65177)

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Tue Sep 12 11:59:42 PDT 2023


================
@@ -0,0 +1,23 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// <memory>
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17
+
+// template <class Ptr> constexpr auto to_address(const Ptr& p) noexcept;
+//     Mandates: one of pointer_traits<Ptr>::to_address() or Ptr::operator->()
+//     is present.
+
+#include <memory>
+
+struct NotPtr {};
+
+void test() {
+  (void)std::to_address(NotPtr()); // expected-error@*:* {{no matching function for call to 'to_address'}}
----------------
ldionne wrote:

I would suggest putting this test in `libcxx/test/std/utilities/memory/pointer.conversion/to_address.pass.cpp` and implementing it like this instead:

```
template <class T>
concept has_to_address = requires(T t) {
  std::to_address(t);
};

static_assert(!has_to_address<NotPtr>);
```

This way, you are actually checking that `to_address` will SFINAE-away if it is passed something that's not a pointer. With your current `.verify.cpp` test, the test would also pass if there was a hard error while trying to instantiate this and it wasn't SFINAE friendly. Does this make sense?

Note that you'll need to change the code I put above to avoid requiring concepts, but I was trying to express the idea here.

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


More information about the libcxx-commits mailing list