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

via libcxx-commits libcxx-commits at lists.llvm.org
Fri Sep 1 13:15:59 PDT 2023


================
@@ -0,0 +1,48 @@
+//===----------------------------------------------------------------------===//
+//
+// 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;
+//     Should not require a specialization of pointer_traits for Ptr.
+
+#include <memory>
+
+struct IntPtr {
+  constexpr int* operator->() const { return ptr; }
+
+  int* ptr;
+};
+
----------------
EricWF wrote:

You need to add some tests for the actual SFINAE friendliness of the type. You'll need some sort of metafunction that allows you to test code in a SFINAE context. For example:

```
template <template <class...> class Templ, class Ignore, class... Args>
struct is_valid_expansion_impl
{
    typedef std::false_type type;
};

template <template <class...> class Templ, class... Args>
struct is_valid_expansion_impl<Templ, decltype((void) Templ<Args...>{}, 0), Args...>
{
    typedef std::true_type type;
};

template <template <class...> class Templ, class... Args>
using is_valid_expansion = typename is_valid_expansion_impl<Templ, int, Args...>::type;


template <class Ptr>
using TestToAddressCall = decltype(std::to_address(std::declval<Ptr>()))

static_assert(is_valid_expansion<TestToAddress, IntPtr>>::value);
static_assert(not is_valid_expansion<TestToAddress, std::string>>::value); 
```


At least that's the least ugly way I can think to test to_address without using concepts

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


More information about the libcxx-commits mailing list