20 #include <type_traits>
29 const char*
what() const noexcept
override
31 return "bad any cast";
61 rhs.vtable->
move(rhs.storage, this->storage);
76 template<typename ValueType, typename = typename std::enable_if<!std::is_same<typename std::decay<ValueType>::type,
any>::value>
::type>
77 any(ValueType&& value)
79 static_assert(std::is_copy_constructible<
typename std::decay<ValueType>::type>::value,
80 "T shall satisfy the CopyConstructible requirements.");
81 this->
construct(std::forward<ValueType>(value));
97 any(std::move(rhs)).swap(*
this);
105 template<typename ValueType, typename = typename std::enable_if<!std::is_same<typename std::decay<ValueType>::type,
any>::value>
::type>
108 static_assert(std::is_copy_constructible<
typename std::decay<ValueType>::type>::value,
109 "T shall satisfy the CopyConstructible requirements.");
110 any(std::forward<ValueType>(value)).swap(*
this);
127 return this->
vtable ==
nullptr;
131 const std::type_info&
type() const noexcept
139 if(this->
vtable != rhs.vtable)
141 any tmp(std::move(rhs));
144 rhs.vtable = this->
vtable;
145 if(this->
vtable !=
nullptr)
161 if(this->
vtable !=
nullptr)
170 using stack_storage_t =
typename std::aligned_storage<2 *
sizeof(
void*), std::alignment_of<void*>::value>::
type;
183 const std::type_info& (*type)() noexcept;
205 static const std::type_info&
type() noexcept
223 dest.dynamic = src.dynamic;
224 src.dynamic =
nullptr;
238 static const std::type_info&
type() noexcept
250 new (&dest.
stack) T(
reinterpret_cast<const T&
>(src.
stack));
257 new (&dest.stack) T(std::move(
reinterpret_cast<T&
>(src.stack)));
263 std::swap(
reinterpret_cast<T&
>(lhs.stack),
reinterpret_cast<T&
>(rhs.stack));
270 std::integral_constant<bool,
271 !(std::is_nothrow_move_constructible<T>::value
272 && sizeof(T) <= sizeof(storage_union::stack)
273 && std::alignment_of<T>::value <= std::alignment_of<storage_union::stack_storage_t>::value)>
278 static vtable_type* vtable_for_type()
280 using VTableType = typename std::conditional<requires_allocation<T>::value, vtable_dynamic<T>, vtable_stack<T>>::type;
281 static vtable_type table = {
282 VTableType::type, VTableType::destroy,
283 VTableType::copy, VTableType::move,
291 friend const T* any_cast(const any* operand) noexcept;
293 friend T* any_cast(any* operand) noexcept;
296 bool is_typed(const std::type_info& t) const
298 return is_same(this->type(), t);
307 static bool is_same(const std::type_info& a, const std::type_info& b)
309 #ifdef ANY_IMPL_FAST_TYPE_INFO_COMPARE
318 const T* cast() const noexcept
320 return requires_allocation<typename std::decay<T>::type>::value?
321 reinterpret_cast<const T*>(storage.dynamic) :
322 reinterpret_cast<const T*>(&storage.stack);
329 return requires_allocation<typename std::decay<T>::type>::value?
330 reinterpret_cast<T*>(storage.dynamic) :
331 reinterpret_cast<T*>(&storage.stack);
335 storage_union storage;
338 template<typename ValueType, typename T>
339 typename std::enable_if<requires_allocation<T>::value>::type
340 do_construct(ValueType&& value)
342 storage.dynamic = new T(std::forward<ValueType>(value));
345 template<typename ValueType, typename T>
346 typename std::enable_if<!requires_allocation<T>::value>::type
347 do_construct(ValueType&& value)
349 new (&storage.stack) T(std::forward<ValueType>(value));
354 template<typename ValueType>
355 void construct(ValueType&& value)
357 using T = typename std::decay<ValueType>::type;
361 do_construct<ValueType,T>(std::forward<ValueType>(value));
369 template<
typename ValueType>
372 return std::move(*p);
375 template<
typename ValueType>
383 template<
typename ValueType>
386 auto p = any_cast<typename std::add_const<typename std::remove_reference<ValueType>::type>
::type>(&operand);
392 template<
typename ValueType>
395 auto p = any_cast<typename std::remove_reference<ValueType>::type>(&operand);
409 template<
typename ValueType>
412 #ifdef ANY_IMPL_ANY_CAST_MOVEABLE
414 using can_move = std::integral_constant<bool,
415 std::is_move_constructible<ValueType>::value
416 && !std::is_lvalue_reference<ValueType>::value>;
421 auto p = any_cast<typename std::remove_reference<ValueType>::type>(&operand);
423 return detail::any_cast_move_if_true<ValueType>(p, can_move());
431 if(operand ==
nullptr || !operand->is_typed(
typeid(T)))
434 return operand->cast<T>();
442 if(operand ==
nullptr || !operand->is_typed(
typeid(T)))
445 return operand->cast<T>();