20 #include <type_traits>
28 const char*
what() const noexcept
override
30 return "bad any cast";
57 rhs.vtable->
move(rhs.storage, this->storage);
72 template <
typename ValueType,
typename =
typename std::enable_if<!std::is_same<
73 typename std::decay<ValueType>::type,
any>::value>
::type>
74 any(ValueType&& value)
76 static_assert(std::is_copy_constructible<
typename std::decay<ValueType>::type>::value,
77 "T shall satisfy the CopyConstructible requirements.");
78 this->
construct(std::forward<ValueType>(value));
94 any(std::move(rhs)).swap(*
this);
102 template <
typename ValueType,
typename =
typename std::enable_if<!std::is_same<
103 typename std::decay<ValueType>::type,
any>::value>
::type>
106 static_assert(std::is_copy_constructible<
typename std::decay<ValueType>::type>::value,
107 "T shall satisfy the CopyConstructible requirements.");
108 any(std::forward<ValueType>(value)).swap(*
this);
125 return this->
vtable ==
nullptr;
129 const std::type_info&
type() const noexcept
137 if (this->
vtable != rhs.vtable)
139 any tmp(std::move(rhs));
142 rhs.vtable = this->
vtable;
143 if (this->
vtable !=
nullptr)
151 if (tmp.
vtable !=
nullptr)
159 if (this->
vtable !=
nullptr)
168 typename std::aligned_storage<2 *
sizeof(
void*), std::alignment_of<void*>::value>::
type;
181 const std::type_info& (*type)() noexcept;
200 template <
typename T>
203 static const std::type_info&
type() noexcept
221 dest.dynamic = src.dynamic;
222 src.dynamic =
nullptr;
233 template <
typename T>
236 static const std::type_info&
type() noexcept
248 new (&dest.
stack) T(
reinterpret_cast<const T&
>(src.
stack));
255 new (&dest.stack) T(std::move(
reinterpret_cast<T&
>(src.stack)));
262 move(rhs, tmp_storage);
264 move(tmp_storage, lhs);
269 template <
typename T>
271 : std::integral_constant<
273 !(std::is_nothrow_move_constructible<T>::value
274 && sizeof(T) <= sizeof(storage_union::stack) &&
275 std::alignment_of<T>::value <=
276 std::alignment_of<storage_union::stack_storage_t>::value)>
281 template <typename T>
282 static vtable_type* vtable_for_type()
284 using VTableType = typename std::conditional<requires_allocation<T>::value,
285 vtable_dynamic<T>, vtable_stack<T>>::type;
286 static vtable_type table = {
287 VTableType::type, VTableType::destroy, VTableType::copy,
288 VTableType::move, VTableType::swap,
294 template <typename T>
295 friend const T* any_cast(const any* operand) noexcept;
296 template <typename T>
297 friend T* any_cast(any* operand) noexcept;
300 bool is_typed(const std::type_info& t) const
302 return is_same(this->type(), t);
311 static bool is_same(const std::type_info& a, const std::type_info& b)
313 #ifdef ANY_IMPL_FAST_TYPE_INFO_COMPARE
321 template <typename T>
322 const T* cast() const noexcept
324 return requires_allocation<typename std::decay<T>::type>::value ?
325 reinterpret_cast<const T*>(storage.dynamic) :
326 reinterpret_cast<const T*>(&storage.stack);
330 template <typename T>
333 return requires_allocation<typename std::decay<T>::type>::value ?
334 reinterpret_cast<T*>(storage.dynamic) :
335 reinterpret_cast<T*>(&storage.stack);
339 storage_union storage;
342 template <typename ValueType, typename T>
343 typename std::enable_if<requires_allocation<T>::value>::type do_construct(ValueType&& value)
345 storage.dynamic = new T(std::forward<ValueType>(value));
348 template <typename ValueType, typename T>
349 typename std::enable_if<!requires_allocation<T>::value>::type do_construct(ValueType&& value)
351 new (&storage.stack) T(std::forward<ValueType>(value));
356 template <typename ValueType>
357 void construct(ValueType&& value)
359 using T = typename std::decay<ValueType>::type;
363 do_construct<ValueType, T>(std::forward<ValueType>(value));
369 template <
typename ValueType>
373 return std::move(*p);
376 template <
typename ValueType>
385 template <
typename ValueType>
389 any_cast<typename std::add_const<typename std::remove_reference<ValueType>::type>
::type>(
397 template <
typename ValueType>
400 auto p = any_cast<typename std::remove_reference<ValueType>::type>(&operand);
415 template <
typename ValueType>
418 #ifdef ANY_IMPL_ANY_CAST_MOVEABLE
420 using can_move = std::integral_constant<bool, std::is_move_constructible<ValueType>::value &&
421 !std::is_lvalue_reference<ValueType>::value>;
423 using can_move = std::false_type;
426 auto p = any_cast<typename std::remove_reference<ValueType>::type>(&operand);
429 return detail::any_cast_move_if_true<ValueType>(p, can_move());
434 template <
typename T>
437 if (operand ==
nullptr || !operand->is_typed(
typeid(T)))
440 return operand->cast<T>();
445 template <
typename T>
448 if (operand ==
nullptr || !operand->is_typed(
typeid(T)))
451 return operand->cast<T>();