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_
12 // Implementation of C++20's std::to_address.
13 // Note: This does consider specializations of pointer_traits<>::to_address,
14 // even though it's a C++20 member function, because CheckedContiguousIterator
15 // specializes pointer_traits<> with a to_address() member.
17 // Reference: https://wg21.link/pointer.conversion#lib:to_address
19 constexpr T* to_address(T* p) noexcept {
20 static_assert(!std::is_function<T>::value,
21 "Error: T must not be a function type.");
25 template <typename Ptr>
26 constexpr auto to_address(const Ptr& p) noexcept
27 -> decltype(std::pointer_traits<Ptr>::to_address(p)) {
28 return std::pointer_traits<Ptr>::to_address(p);
31 template <typename Ptr, typename... None>
32 constexpr auto to_address(const Ptr& p, None...) noexcept {
33 return base::to_address(p.operator->());
38 #endif // BASE_CXX20_TO_ADDRESS_H_