28 #ifndef YB_INC_ystdex_cast_hpp_
29 #define YB_INC_ystdex_cast_hpp_ 1
35 #include <initializer_list>
54 template<
typename _tDst,
typename _tSrc>
58 static_assert(is_pod<_tDst>::value,
"Non-POD destination type found.");
59 static_assert(
sizeof(_tSrc) ==
sizeof(_tDst),
"Incompatible types found.");
76 template<
typename _tDst,
typename... _tSrc>
77 yconstfn std::initializer_list<_tDst>
94 template<
typename _pDst,
class _tSrc>
98 static_assert(is_polymorphic<_tSrc>::value,
"Non-polymorphic class found.");
99 static_assert(is_pointer<_pDst>::value,
"Non-pointer destination found.");
101 const auto tmp(dynamic_cast<_pDst>(x));
104 throw std::bad_cast();
122 template<
typename _pDst,
class _tSrc>
126 static_assert(is_polymorphic<_tSrc>::value,
"Non-polymorphic class found.");
127 static_assert(is_pointer<_pDst>::value,
"Non-pointer destination found.");
131 yassume(dynamic_cast<_pDst>(x) == x);
132 return static_cast<_pDst
>(x);
140 template<
typename _rDst,
class _tSrc>
144 static_assert(is_lvalue_reference<_rDst>::value,
145 "Invalid destination type found.");
147 return *ystdex::polymorphic_downcast<remove_reference_t<_rDst>*>(
156 template<
typename _rDst,
class _tSrc>
157 yconstfn enable_if_t<!is_reference<_tSrc>::value, _rDst>
160 static_assert(is_rvalue_reference<_rDst>::value,
161 "Invalid destination found.");
163 return std::move(ystdex::polymorphic_downcast<_rDst&>(x));
182 template<
typename _pDst,
class _tSrc>
186 static_assert(is_polymorphic<_tSrc>::value,
"Non-polymorphic class found.");
187 static_assert(is_pointer<_pDst>::value,
"Non-pointer destination found.");
189 auto p(dynamic_cast<_pDst>(x));
200 template<
typename _rDst,
class _tSrc>
204 static_assert(is_lvalue_reference<_rDst>::value,
205 "Invalid destination type found.");
207 return *ystdex::polymorphic_crosscast<remove_reference_t<_rDst>*>(
216 template<
typename _rDst,
class _tSrc>
217 yconstfn enable_if_t<!is_reference<_tSrc>::value, _rDst>
220 static_assert(is_rvalue_reference<_rDst>::value,
221 "Invalid destination type found.");
223 return std::move(ystdex::polymorphic_crosscast<_rDst&>(x));
233 template<
typename _tFrom,
typename _tTo,
bool _bNonVirtualDownCast>
239 return ystdex::polymorphic_downcast<_tTo>(x);
243 template<
typename _tFrom,
typename _tTo>
249 return dynamic_cast<_tTo
>(x);
254 template<
typename _tFrom,
typename _tTo,
bool _bUseStaticCast>
264 template<
typename _tFrom,
typename _tTo>
277 template<
typename _type>
287 template<
typename _type>
297 template<
typename _tFrom,
typename _tTo>
299 : integral_constant<bool, is_convertible<_tFrom, _tTo>::value>
318 template<
typename _tDst,
typename _tSrc>
325 template<
typename _tDst,
typename _tSrc>
332 template<
typename _tDst,
typename _tSrc>
_tDst union_cast(_tSrc x)
使用匿名联合体进行的类型转换。
static _type cast(_type x)
_pDst polymorphic_cast(_tSrc *x)
多态类指针类型转换。
_tDst general_cast(_tSrc *x)
一般类型转换。
static _tTo cast(_tFrom x)
#define yforward(_expr)
根据参数类型使用 std::forward 传递对应参数。
typename remove_pointer< _type >::type remove_pointer_t
static _tTo cast(_tFrom x)
static _type cast(_type x)
_pDst polymorphic_crosscast(_tSrc *x)
多态类指针交叉转换。
remove_pointer_t< remove_reference_t< _type >> type
#define ynothrow
YSLib 无异常抛出保证:若支持 noexcept 关键字, 指定特定的 noexcept 异常规范。
_pDst polymorphic_downcast(_tSrc *x)
多态类指针向派生类指针转换。
std::initializer_list< _tDst > initializer_cast(_tSrc &&...x)
初值符列表转换。
typename remove_cv< _type >::type remove_cv_t
static _tTo cast(_tFrom x)
#define yconstfn
指定编译时常量函数。
static _tTo cast(_tFrom x)