Horizon
addressof.hpp
1 // Range v3 library
2 //
3 // Copyright Eric Niebler 2013-present
4 //
5 // Use, modification and distribution is subject to the
6 // Boost Software License, Version 1.0. (See accompanying
7 // file LICENSE_1_0.txt or copy at
8 // http://www.boost.org/LICENSE_1_0.txt)
9 //
10 // Project home: https://github.com/ericniebler/range-v3
11 //
12 #ifndef RANGES_V3_UTILITY_ADDRESSOF_HPP
13 #define RANGES_V3_UTILITY_ADDRESSOF_HPP
14 
15 #include <memory>
16 #include <type_traits>
17 
18 #include <concepts/concepts.hpp>
19 
20 #include <range/v3/range_fwd.hpp>
21 
22 #include <range/v3/detail/config.hpp>
23 
24 #include <range/v3/detail/prologue.hpp>
25 
26 namespace ranges
27 {
29  namespace detail
30  {
31 #ifdef __cpp_lib_addressof_constexpr
32  using std::addressof;
33 #else
34  namespace check_addressof
35  {
36  inline ignore_t operator&(ignore_t)
37  {
38  return {};
39  }
40  template<typename T>
41  auto addressof(T & t)
42  {
43  return &t;
44  }
45  } // namespace check_addressof
46 
47  template<typename T>
48  constexpr bool has_bad_addressof()
49  {
50  return !std::is_scalar<T>::value &&
51  !RANGES_IS_SAME(decltype(check_addressof::addressof(*(T *)nullptr)),
52  ignore_t);
53  }
54 
55  template(typename T)(
56  requires(has_bad_addressof<T>()))
57  T * addressof(T & arg) noexcept
58  {
59  return std::addressof(arg);
60  }
61 
62  template(typename T)(
63  requires (!has_bad_addressof<T>()))
64  constexpr T * addressof(T & arg) noexcept
65  {
66  return &arg;
67  }
68 
69  template<typename T>
70  T const * addressof(T const &&) = delete;
71 #endif
72  } // namespace detail
74 } // namespace ranges
75 
76 #include <range/v3/detail/epilogue.hpp>
77 
78 #endif