blob: 04d0f71d7235b204a22f218f266346b5c5aa318e [file] [log] [blame]
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <memory>
#include <type_traits>
namespace gurl_base {
namespace {
template <typename Ptr, typename = void>
struct has_std_to_address : std::false_type {};
template <typename Ptr>
struct has_std_to_address<
std::declval<Ptr>()))>> : std::true_type {};
} // namespace
// Implementation of C++20's std::to_address.
// Note: This does consider specializations of pointer_traits<>::to_address,
// even though it's a C++20 member function, because CheckedContiguousIterator
// specializes pointer_traits<> with a to_address() member.
// Reference:
template <typename T>
constexpr T* to_address(T* p) noexcept {
"Error: T must not be a function type.");
return p;
template <typename Ptr>
constexpr auto to_address(const Ptr& p) noexcept {
if constexpr (has_std_to_address<Ptr>::value) {
return std::pointer_traits<Ptr>::to_address(p);
} else {
return gurl_base::to_address(p.operator->());
} // namespace base
#endif // BASE_CXX20_TO_ADDRESS_H_