VCCC  2024.05
VisualCamp Common C++ library
to_address.hpp
Go to the documentation of this file.
1 //
2 // Created by yonggyulee on 2023/12/07.
3 //
4 
5 #ifndef VCCC_MEMORY_TO_ADDRESS_HPP_
6 #define VCCC_MEMORY_TO_ADDRESS_HPP_
7 
8 #include <memory>
9 #include <type_traits>
10 
14 
15 namespace vccc {
16 namespace detail {
17 
18 template<typename T, typename = void>
19 struct has_to_address : std::false_type {};
20 
21 template<typename T>
22 struct has_to_address<T, void_t<decltype(pointer_traits<T>::to_address(std::declval<const T&>()))>> : std::true_type {};
23 
24 template<typename T>
25 constexpr auto to_address_fancy(const T& p, std::true_type /* has_to_address */ ) noexcept {
27 }
28 template<typename T>
29 constexpr auto to_address_fancy(const T& p, std::false_type /* has_to_address */ ) noexcept;
30 
31 } // namespace detail
32 
35 
36 template<class T>
37 constexpr T* to_address(T* p) noexcept {
38  static_assert(!std::is_function<T>::value, "T must not be a pointer to function");
39  return p;
40 }
41 
42 template<class T>
43 constexpr auto to_address(const T& p) noexcept {
44  return detail::to_address_fancy(p, detail::has_to_address<T>{});
45 }
46 
48 
49 namespace detail {
50 
51 template<typename T>
52 constexpr auto to_address_fancy(const T& p, std::false_type /* has_to_address */ ) noexcept {
53  return vccc::to_address(p.operator->());
54 }
55 
56 } // namespace detail
57 
58 } // namespace vccc
59 
60 #endif // VCCC_MEMORY_TO_ADDRESS_HPP_
constexpr T * to_address(T *p) noexcept
Definition: to_address.hpp:37
constexpr auto to_address(const T &p) noexcept
Definition: to_address.hpp:43
void void_t
Definition: void_t.hpp:19
Definition: directory.h:12
constexpr VCCC_INLINE_OR_STATIC detail::element_niebloid< 1 > value
Definition: key_value.hpp:35