YSTest: addressof.hpp 源文件

YSTest PreAlpha_b900_20201012 The YSLib Test Project

addressof.hpp
浏览该文件的文档.
1 /*
2 © 2015-2016, 2018-2019 FrankHB.
3
4 This file is part of the YSLib project, and may only be used,
5 modified, and distributed under the terms of the YSLib project
6 license, LICENSE.TXT. By continuing to use, modify, or distribute
7 this file you indicate that you have read the license and
8 understand and accept it fully.
9 */
10
32 #ifndef YB_INC_ystdex_addressof_hpp_
33 #define YB_INC_ystdex_addressof_hpp_ 1
34
35 #include "meta.hpp" // for internal "meta.hpp", std::declval, is_detected, or_;
36 #include <memory> // for std::addressof;
37
45 // XXX: There was once no feature test macro provided. See http://www.open-std.org/pipermail/features/2016-March/000399.html.
46 // XXX: It is known 'std::addressof' has 'constexpr' in Microsoft VC++ 15.6.4.
47 // Not sure which is the initial version as it seems undocumented.
48 // TODO: Get more accurate version.
49 #ifndef __cpp_lib_addressof_constexpr
50 # if (YB_IMPL_MSCPP >= 1913 && _MSVC_LANG >= 201606) || __cplusplus >= 201606L
51 # define __cpp_lib_addressof_constexpr 201606L
52 # endif
53 #endif
54
55
56 namespace ystdex
57 {
58
59 namespace details
60 {
61
63
64 template<typename _type>
65 using addressof_mem_t = decltype(std::declval<const _type&>().operator&());
66
67 template<typename _type>
68 using addressof_free_t = decltype(operator&(std::declval<const _type&>()));
70
71 } // namespace details;
72
74
75
80 template<typename _type>
81 struct has_overloaded_addressof
82 : or_<is_detected<details::addressof_mem_t, _type>,
83 is_detected<details::addressof_free_t, _type>>
84 {};
85
86
92 template<typename _type>
93 struct has_no_overloaded_addressof
94 : or_<is_arithmetic<remove_cvref_t<_type>>, is_array<remove_cvref_t<_type>>,
95 is_function<remove_reference_t<_type>>, is_null_pointer<_type>>
96 {};
98
99
112 #if __cpp_lib_addressof_constexpr >= 201606L
113 using std::addressof;
114 #else
115 // NOTE: This is implemented since GCC r240873.
116 # if __has_builtin(__builtin_addressof) || YB_IMPL_GNUCPP >= 70100 \
117 || YB_IMPL_CLANGPP >= 30300
118 template<typename _type>
120 YB_ATTR_nodiscard YB_PURE yconstfn _type*
121 addressof(_type& r) ynothrow
122 {
123 return __builtin_addressof(r);
124 }
125 # else
126
128 template<typename _type>
129 YB_ATTR_nodiscard YB_PURE yconstfn
130 yimpl(enable_if_t)<has_no_overloaded_addressof<_type>::value, _type*>
131 addressof(_type& r) ynothrow
132 {
133 return &r;
134 }
139 template<typename _type,
140 yimpl(typename = enable_if_t<!has_no_overloaded_addressof<_type>::value>)>
141 YB_ATTR_nodiscard YB_PURE inline _type*
142 addressof(_type& r) ynothrow
143 {
144 return std::addressof(r);
145 }
147 # endif
148 template<class _type>
150 const _type*
151 addressof(const _type&&) = delete;
152 #endif
153
154
155
164 template<typename _type>
165 yconstfn yimpl(enable_if_t)<or_<is_object<_type>, is_void<_type>>::value, void*>
166 pvoid(_type* p) ynothrow
167 {
168 return const_cast<void*>(static_cast<const volatile void*>(p));
169 }
171 template<typename _type>
172 inline yimpl(enable_if_t)<is_function<_type>::value, void*>
173 pvoid(_type* p) ynothrow
174 {
175 return const_cast<void*>(reinterpret_cast<const volatile void*>(p));
176 }
177
182 template<typename _type>
183 yconstfn void*
184 pvoid_ref(_type&& ref)
185 {
186 return ystdex::pvoid(std::addressof(ref));
187 }
189
190 } // namespace ystdex;
191
192 #endif
193
meta.hpp
通用元编程设施。
YB_ATTR_nodiscard
#define YB_ATTR_nodiscard
标记返回值不被忽略的属性。
Definition: ydef.h:715
ystdex::addressof
const _type * addressof(const _type &&)=delete
YB_PURE
#define YB_PURE
指示函数或函数模板实例为纯函数。
Definition: ydef.h:919
ystdex::details::addressof_mem_t
decltype(std::declval< const _type & >().operator&()) addressof_mem_t
Definition: addressof.hpp:65
YSLib::Drawing::r
The boundary is out of the const Graphics const Rect const Point const Point Color c const Rect const Rect & r
Definition: YDraw.h:174
ystdex::cpp2014::enable_if_t
typename enable_if< _bCond, _type >::type enable_if_t
Definition: meta.hpp:199
ystdex::pvoid_ref
void * pvoid_ref(_type &&ref)
Definition: addressof.hpp:184
ystdex::has_no_overloaded_addressof
判断是否无法重载 & 操作符接受指定类型的表达式。
Definition: addressof.hpp:96
ystdex::or_
逻辑或。
Definition: integral_constant.hpp:172
ystdex::details::addressof_free_t
decltype(operator&(std::declval< const _type & >())) addressof_free_t
Definition: addressof.hpp:68
ystdex::pvoid
enable_if_t< or_< is_object< _type >, is_void< _type > >::value, void * > pvoid(_type *p)
取 void* 类型的指针。
Definition: addressof.hpp:166
ystdex::has_overloaded_addressof
判断是否存在合式重载 & 操作符接受指定类型的表达式。
Definition: addressof.hpp:84
ystdex
<memory> 特性测试宏。
Definition: ydef.h:1262
ystdex::addressof
enable_if_t< has_no_overloaded_addressof< _type >::value, _type * > addressof(_type &r)
尝试对非重载 operator& 提供 constexpr 的 std::addressof 替代。
Definition: addressof.hpp:131
platform::p
return p p
Definition: YCLib/FileSystem.h:209
yimpl
#define yimpl(...)
实现标签。
Definition: ydef.h:413
ynothrow
#define ynothrow
YSLib 无异常抛出保证：若支持 noexcept 关键字， 指定特定的 noexcept 异常规范。
Definition: ydef.h:1199
ystdex::ref
void ref(const _type &&)=delete
yconstfn
#define yconstfn
指定编译时常量函数。
Definition: ydef.h:1073