cxx20_to_address.h 912 B

12345678910111213141516171819202122232425262728293031
  1. // Copyright 2021 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef BASE_CXX20_TO_ADDRESS_H_
  5. #define BASE_CXX20_TO_ADDRESS_H_
  6. #include <type_traits>
  7. namespace base {
  8. // Simplified C++14 implementation of C++20's std::to_address.
  9. // Note: This does not consider specializations of pointer_traits<>::to_address,
  10. // since that member function may only be present in C++20 and later.
  11. //
  12. // Reference: https://wg21.link/pointer.conversion#lib:to_address
  13. template <typename T>
  14. constexpr T* to_address(T* p) noexcept {
  15. static_assert(!std::is_function<T>::value,
  16. "Error: T must not be a function type.");
  17. return p;
  18. }
  19. template <typename Ptr>
  20. constexpr auto to_address(const Ptr& p) noexcept {
  21. return to_address(p.operator->());
  22. }
  23. } // namespace base
  24. #endif // BASE_CXX20_TO_ADDRESS_H_