3// Copyright (C) 2014-2021 Free Software Foundation, Inc.
5// This file is part of the GNU ISO C++ Library. This library is free
6// software; you can redistribute it and/or modify it under the
7// terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 3, or (at your option)
11// This library is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
16// Under Section 7 of GPL version 3, you are granted additional
17// permissions described in the GCC Runtime Library Exception, version
18// 3.1, as published by the Free Software Foundation.
20// You should have received a copy of the GNU General Public License and
21// a copy of the GCC Runtime Library Exception along with this program;
22// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23// <http://www.gnu.org/licenses/>.
26 * This is a Standard C++ Library header.
32#pragma GCC system_header
34#if __cplusplus >= 201703L
41namespace std _GLIBCXX_VISIBILITY(default)
43_GLIBCXX_BEGIN_NAMESPACE_VERSION
46 * @addtogroup utilities
51 * @brief Exception class thrown by a failed @c any_cast
54 class bad_any_cast : public bad_cast
57 virtual const char* what() const noexcept { return "bad any_cast"; }
60 [[gnu::noreturn]] inline void __throw_bad_any_cast()
69#define __cpp_lib_any 201606L
72 * @brief A type-safe container of any type.
74 * An `any` object's state is either empty or it stores a contained object
75 * of CopyConstructible type.
81 // Holds either pointer to a heap object or the contained object itself.
84 constexpr _Storage() : _M_ptr{nullptr} {}
86 // Prevent trivial copies of this type, buffer might hold a non-POD.
87 _Storage(const _Storage&) = delete;
88 _Storage& operator=(const _Storage&) = delete;
91 aligned_storage<sizeof(_M_ptr), alignof(void*)>::type _M_buffer;
94 template<typename _Tp, typename _Safe = is_nothrow_move_constructible<_Tp>,
95 bool _Fits = (sizeof(_Tp) <= sizeof(_Storage))
96 && (alignof(_Tp) <= alignof(_Storage))>
97 using _Internal = std::integral_constant<bool, _Safe::value && _Fits>;
99 template<typename _Tp>
100 struct _Manager_internal; // uses small-object optimization
102 template<typename _Tp>
103 struct _Manager_external; // creates contained object on the heap
105 template<typename _Tp>
106 using _Manager = conditional_t<_Internal<_Tp>::value,
107 _Manager_internal<_Tp>,
108 _Manager_external<_Tp>>;
110 template<typename _Tp, typename _VTp = decay_t<_Tp>>
111 using _Decay_if_not_any = enable_if_t<!is_same_v<_VTp, any>, _VTp>;
113 /// Emplace with an object created from @p __args as the contained object.
114 template <typename _Tp, typename... _Args,
115 typename _Mgr = _Manager<_Tp>>
116 void __do_emplace(_Args&&... __args)
119 _Mgr::_S_create(_M_storage, std::forward<_Args>(__args)...);
120 _M_manager = &_Mgr::_S_manage;
123 /// Emplace with an object created from @p __il and @p __args as
124 /// the contained object.
125 template <typename _Tp, typename _Up, typename... _Args,
126 typename _Mgr = _Manager<_Tp>>
127 void __do_emplace(initializer_list<_Up> __il, _Args&&... __args)
130 _Mgr::_S_create(_M_storage, __il, std::forward<_Args>(__args)...);
131 _M_manager = &_Mgr::_S_manage;
134 template <typename _Res, typename _Tp, typename... _Args>
135 using __any_constructible
136 = enable_if<__and_<is_copy_constructible<_Tp>,
137 is_constructible<_Tp, _Args...>>::value,
140 template <typename _Tp, typename... _Args>
141 using __any_constructible_t
142 = typename __any_constructible<bool, _Tp, _Args...>::type;
144 template<typename _VTp, typename... _Args>
146 = typename __any_constructible<_VTp&, _VTp, _Args...>::type;
149 // construct/destruct
151 /// Default constructor, creates an empty object.
152 constexpr any() noexcept : _M_manager(nullptr) { }
154 /// Copy constructor, copies the state of @p __other
155 any(const any& __other)
157 if (!__other.has_value())
158 _M_manager = nullptr;
163 __other._M_manager(_Op_clone, &__other, &__arg);
168 * @brief Move constructor, transfer the state from @p __other
170 * @post @c !__other.has_value() (this postcondition is a GNU extension)
172 any(any&& __other) noexcept
174 if (!__other.has_value())
175 _M_manager = nullptr;
180 __other._M_manager(_Op_xfer, &__other, &__arg);
184 /// Construct with a copy of @p __value as the contained object.
185 template <typename _Tp, typename _VTp = _Decay_if_not_any<_Tp>,
186 typename _Mgr = _Manager<_VTp>,
187 typename = _Require<__not_<__is_in_place_type<_VTp>>,
188 is_copy_constructible<_VTp>>>
190 : _M_manager(&_Mgr::_S_manage)
192 _Mgr::_S_create(_M_storage, std::forward<_Tp>(__value));
195 /// Construct with an object created from @p __args as the contained object.
196 template <typename _Tp, typename... _Args, typename _VTp = decay_t<_Tp>,
197 typename _Mgr = _Manager<_VTp>,
198 __any_constructible_t<_VTp, _Args&&...> = false>
200 any(in_place_type_t<_Tp>, _Args&&... __args)
201 : _M_manager(&_Mgr::_S_manage)
203 _Mgr::_S_create(_M_storage, std::forward<_Args>(__args)...);
206 /// Construct with an object created from @p __il and @p __args as
207 /// the contained object.
208 template <typename _Tp, typename _Up, typename... _Args,
209 typename _VTp = decay_t<_Tp>, typename _Mgr = _Manager<_VTp>,
210 __any_constructible_t<_VTp, initializer_list<_Up>&,
213 any(in_place_type_t<_Tp>, initializer_list<_Up> __il, _Args&&... __args)
214 : _M_manager(&_Mgr::_S_manage)
216 _Mgr::_S_create(_M_storage, __il, std::forward<_Args>(__args)...);
219 /// Destructor, calls @c reset()
224 /// Copy the state of another object.
226 operator=(const any& __rhs)
233 * @brief Move assignment operator
235 * @post @c !__rhs.has_value() (not guaranteed for other implementations)
238 operator=(any&& __rhs) noexcept
240 if (!__rhs.has_value())
242 else if (this != &__rhs)
247 __rhs._M_manager(_Op_xfer, &__rhs, &__arg);
252 /// Store a copy of @p __rhs as the contained object.
253 template<typename _Tp>
254 enable_if_t<is_copy_constructible<_Decay_if_not_any<_Tp>>::value, any&>
255 operator=(_Tp&& __rhs)
257 *this = any(std::forward<_Tp>(__rhs));
261 /// Emplace with an object created from @p __args as the contained object.
262 template <typename _Tp, typename... _Args>
263 __emplace_t<decay_t<_Tp>, _Args...>
264 emplace(_Args&&... __args)
266 using _VTp = decay_t<_Tp>;
267 __do_emplace<_VTp>(std::forward<_Args>(__args)...);
268 return *any::_Manager<_VTp>::_S_access(_M_storage);
271 /// Emplace with an object created from @p __il and @p __args as
272 /// the contained object.
273 template <typename _Tp, typename _Up, typename... _Args>
274 __emplace_t<decay_t<_Tp>, initializer_list<_Up>&, _Args&&...>
275 emplace(initializer_list<_Up> __il, _Args&&... __args)
277 using _VTp = decay_t<_Tp>;
278 __do_emplace<_VTp, _Up>(__il, std::forward<_Args>(__args)...);
279 return *any::_Manager<_VTp>::_S_access(_M_storage);
284 /// If not empty, destroy the contained object.
285 void reset() noexcept
289 _M_manager(_Op_destroy, this, nullptr);
290 _M_manager = nullptr;
294 /// Exchange state with another object.
295 void swap(any& __rhs) noexcept
297 if (!has_value() && !__rhs.has_value())
300 if (has_value() && __rhs.has_value())
307 __arg._M_any = &__tmp;
308 __rhs._M_manager(_Op_xfer, &__rhs, &__arg);
309 __arg._M_any = &__rhs;
310 _M_manager(_Op_xfer, this, &__arg);
312 __tmp._M_manager(_Op_xfer, &__tmp, &__arg);
316 any* __empty = !has_value() ? this : &__rhs;
317 any* __full = !has_value() ? &__rhs : this;
319 __arg._M_any = __empty;
320 __full->_M_manager(_Op_xfer, __full, &__arg);
326 /// Reports whether there is a contained object or not.
327 bool has_value() const noexcept { return _M_manager != nullptr; }
330 /// The @c typeid of the contained object, or @c typeid(void) if empty.
331 const type_info& type() const noexcept
336 _M_manager(_Op_get_type_info, this, &__arg);
337 return *__arg._M_typeinfo;
341 /// @cond undocumented
342 template<typename _Tp>
343 static constexpr bool __is_valid_cast()
344 { return __or_<is_reference<_Tp>, is_copy_constructible<_Tp>>::value; }
349 _Op_access, _Op_get_type_info, _Op_clone, _Op_destroy, _Op_xfer
355 const std::type_info* _M_typeinfo;
359 void (*_M_manager)(_Op, const any*, _Arg*);
362 /// @cond undocumented
363 template<typename _Tp>
364 friend void* __any_caster(const any* __any);
367 // Manage in-place contained object.
368 template<typename _Tp>
369 struct _Manager_internal
372 _S_manage(_Op __which, const any* __anyp, _Arg* __arg);
374 template<typename _Up>
376 _S_create(_Storage& __storage, _Up&& __value)
378 void* __addr = &__storage._M_buffer;
379 ::new (__addr) _Tp(std::forward<_Up>(__value));
382 template<typename... _Args>
384 _S_create(_Storage& __storage, _Args&&... __args)
386 void* __addr = &__storage._M_buffer;
387 ::new (__addr) _Tp(std::forward<_Args>(__args)...);
391 _S_access(const _Storage& __storage)
393 // The contained object is in __storage._M_buffer
394 const void* __addr = &__storage._M_buffer;
395 return static_cast<_Tp*>(const_cast<void*>(__addr));
399 // Manage external contained object.
400 template<typename _Tp>
401 struct _Manager_external
404 _S_manage(_Op __which, const any* __anyp, _Arg* __arg);
406 template<typename _Up>
408 _S_create(_Storage& __storage, _Up&& __value)
410 __storage._M_ptr = new _Tp(std::forward<_Up>(__value));
412 template<typename... _Args>
414 _S_create(_Storage& __storage, _Args&&... __args)
416 __storage._M_ptr = new _Tp(std::forward<_Args>(__args)...);
419 _S_access(const _Storage& __storage)
421 // The contained object is in *__storage._M_ptr
422 return static_cast<_Tp*>(__storage._M_ptr);
427 /// Exchange the states of two @c any objects.
428 inline void swap(any& __x, any& __y) noexcept { __x.swap(__y); }
430 /// Create an `any` holding a `_Tp` constructed from `__args...`.
431 template <typename _Tp, typename... _Args>
433 enable_if_t<is_constructible_v<any, in_place_type_t<_Tp>, _Args...>, any>
434 make_any(_Args&&... __args)
436 return any(in_place_type<_Tp>, std::forward<_Args>(__args)...);
439 /// Create an `any` holding a `_Tp` constructed from `__il` and `__args...`.
440 template <typename _Tp, typename _Up, typename... _Args>
442 enable_if_t<is_constructible_v<any, in_place_type_t<_Tp>,
443 initializer_list<_Up>&, _Args...>, any>
444 make_any(initializer_list<_Up> __il, _Args&&... __args)
446 return any(in_place_type<_Tp>, __il, std::forward<_Args>(__args)...);
450 * @brief Access the contained object.
452 * @tparam _ValueType A const-reference or CopyConstructible type.
453 * @param __any The object to access.
454 * @return The contained object.
455 * @throw bad_any_cast If <code>
456 * __any.type() != typeid(remove_reference_t<_ValueType>)
459 template<typename _ValueType>
460 inline _ValueType any_cast(const any& __any)
462 using _Up = __remove_cvref_t<_ValueType>;
463 static_assert(any::__is_valid_cast<_ValueType>(),
464 "Template argument must be a reference or CopyConstructible type");
465 static_assert(is_constructible_v<_ValueType, const _Up&>,
466 "Template argument must be constructible from a const value.");
467 auto __p = any_cast<_Up>(&__any);
469 return static_cast<_ValueType>(*__p);
470 __throw_bad_any_cast();
474 * @brief Access the contained object.
476 * @tparam _ValueType A reference or CopyConstructible type.
477 * @param __any The object to access.
478 * @return The contained object.
479 * @throw bad_any_cast If <code>
480 * __any.type() != typeid(remove_reference_t<_ValueType>)
485 template<typename _ValueType>
486 inline _ValueType any_cast(any& __any)
488 using _Up = __remove_cvref_t<_ValueType>;
489 static_assert(any::__is_valid_cast<_ValueType>(),
490 "Template argument must be a reference or CopyConstructible type");
491 static_assert(is_constructible_v<_ValueType, _Up&>,
492 "Template argument must be constructible from an lvalue.");
493 auto __p = any_cast<_Up>(&__any);
495 return static_cast<_ValueType>(*__p);
496 __throw_bad_any_cast();
499 template<typename _ValueType>
500 inline _ValueType any_cast(any&& __any)
502 using _Up = __remove_cvref_t<_ValueType>;
503 static_assert(any::__is_valid_cast<_ValueType>(),
504 "Template argument must be a reference or CopyConstructible type");
505 static_assert(is_constructible_v<_ValueType, _Up>,
506 "Template argument must be constructible from an rvalue.");
507 auto __p = any_cast<_Up>(&__any);
509 return static_cast<_ValueType>(std::move(*__p));
510 __throw_bad_any_cast();
514 /// @cond undocumented
515 template<typename _Tp>
516 void* __any_caster(const any* __any)
518 // any_cast<T> returns non-null if __any->type() == typeid(T) and
519 // typeid(T) ignores cv-qualifiers so remove them:
520 using _Up = remove_cv_t<_Tp>;
521 // The contained value has a decayed type, so if decay_t<U> is not U,
522 // then it's not possible to have a contained value of type U:
523 if constexpr (!is_same_v<decay_t<_Up>, _Up>)
525 // Only copy constructible types can be used for contained values:
526 else if constexpr (!is_copy_constructible_v<_Up>)
528 // First try comparing function addresses, which works without RTTI
529 else if (__any->_M_manager == &any::_Manager<_Up>::_S_manage
531 || __any->type() == typeid(_Tp)
535 return any::_Manager<_Up>::_S_access(__any->_M_storage);
542 * @brief Access the contained object.
544 * @tparam _ValueType The type of the contained object.
545 * @param __any A pointer to the object to access.
546 * @return The address of the contained object if <code>
547 * __any != nullptr && __any.type() == typeid(_ValueType)
548 * </code>, otherwise a null pointer.
552 template<typename _ValueType>
553 inline const _ValueType* any_cast(const any* __any) noexcept
555 if constexpr (is_object_v<_ValueType>)
557 return static_cast<_ValueType*>(__any_caster<_ValueType>(__any));
561 template<typename _ValueType>
562 inline _ValueType* any_cast(any* __any) noexcept
564 if constexpr (is_object_v<_ValueType>)
566 return static_cast<_ValueType*>(__any_caster<_ValueType>(__any));
571 template<typename _Tp>
573 any::_Manager_internal<_Tp>::
574 _S_manage(_Op __which, const any* __any, _Arg* __arg)
576 // The contained object is in _M_storage._M_buffer
577 auto __ptr = reinterpret_cast<const _Tp*>(&__any->_M_storage._M_buffer);
581 __arg->_M_obj = const_cast<_Tp*>(__ptr);
583 case _Op_get_type_info:
585 __arg->_M_typeinfo = &typeid(_Tp);
589 ::new(&__arg->_M_any->_M_storage._M_buffer) _Tp(*__ptr);
590 __arg->_M_any->_M_manager = __any->_M_manager;
596 ::new(&__arg->_M_any->_M_storage._M_buffer) _Tp
597 (std::move(*const_cast<_Tp*>(__ptr)));
599 __arg->_M_any->_M_manager = __any->_M_manager;
600 const_cast<any*>(__any)->_M_manager = nullptr;
605 template<typename _Tp>
607 any::_Manager_external<_Tp>::
608 _S_manage(_Op __which, const any* __any, _Arg* __arg)
610 // The contained object is *_M_storage._M_ptr
611 auto __ptr = static_cast<const _Tp*>(__any->_M_storage._M_ptr);
615 __arg->_M_obj = const_cast<_Tp*>(__ptr);
617 case _Op_get_type_info:
619 __arg->_M_typeinfo = &typeid(_Tp);
623 __arg->_M_any->_M_storage._M_ptr = new _Tp(*__ptr);
624 __arg->_M_any->_M_manager = __any->_M_manager;
630 __arg->_M_any->_M_storage._M_ptr = __any->_M_storage._M_ptr;
631 __arg->_M_any->_M_manager = __any->_M_manager;
632 const_cast<any*>(__any)->_M_manager = nullptr;
639 namespace __detail::__variant
641 template<typename> struct _Never_valueless_alt; // see <variant>
643 // Provide the strong exception-safety guarantee when emplacing an
644 // any into a variant.
646 struct _Never_valueless_alt<std::any>
649 } // namespace __detail::__variant
651_GLIBCXX_END_NAMESPACE_VERSION
655#endif // _GLIBCXX_ANY