1 // Copyright 2021 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef BASE_CXX20_TO_ADDRESS_H_
6 #define BASE_CXX20_TO_ADDRESS_H_
15 template <typename Ptr, typename = void>
16 struct has_std_to_address : std::false_type {};
18 template <typename Ptr>
19 struct has_std_to_address<
21 std::void_t<decltype(std::pointer_traits<Ptr>::to_address(
22 std::declval<Ptr>()))>> : std::true_type {};
26 // Implementation of C++20's std::to_address.
27 // Note: This does consider specializations of pointer_traits<>::to_address,
28 // even though it's a C++20 member function, because CheckedContiguousIterator
29 // specializes pointer_traits<> with a to_address() member.
31 // Reference: https://wg21.link/pointer.conversion#lib:to_address
33 constexpr T* to_address(T* p) noexcept {
34 static_assert(!std::is_function_v<T>,
35 "Error: T must not be a function type.");
39 template <typename Ptr>
40 constexpr auto to_address(const Ptr& p) noexcept {
41 if constexpr (has_std_to_address<Ptr>::value) {
42 return std::pointer_traits<Ptr>::to_address(p);
44 return base::to_address(p.operator->());
50 #endif // BASE_CXX20_TO_ADDRESS_H_