1// <experimental/any> -*- C++ -*-
3// Copyright (C) 2014-2025 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/>.
25/** @file experimental/any
26 * This is a TS C++ Library header.
30#ifndef _GLIBCXX_EXPERIMENTAL_ANY
31#define _GLIBCXX_EXPERIMENTAL_ANY 1
34#pragma GCC system_header
37#include <bits/requires_hosted.h> // experimental is currently omitted
39#if __cplusplus >= 201402L
45#include <experimental/bits/lfts_config.h>
47namespace std _GLIBCXX_VISIBILITY(default)
49_GLIBCXX_BEGIN_NAMESPACE_VERSION
53inline namespace fundamentals_v1
56 * @defgroup any Type-safe container of any type
59 * A type-safe container for single values of value types, as
60 * described in n3804 "Any Library Proposal (Revision 3)".
65#define __cpp_lib_experimental_any 201411
68 * @brief Exception class thrown by a failed @c any_cast
71 class bad_any_cast : public bad_cast
74 virtual const char* what() const noexcept { return "bad any_cast"; }
77 /// @cond undocumented
78 [[gnu::noreturn]] inline void __throw_bad_any_cast()
89 * @brief A type-safe container of any type.
91 * An @c any object's state is either empty or it stores a contained object
92 * of CopyConstructible type.
96 // Holds either pointer to a heap object or the contained object itself.
99 // This constructor intentionally doesn't initialize anything.
100 _Storage() = default;
102 // Prevent trivial copies of this type, buffer might hold a non-POD.
103 _Storage(const _Storage&) = delete;
104 _Storage& operator=(const _Storage&) = delete;
107 unsigned char _M_buffer[sizeof(_M_ptr)];
110 template<typename _Tp, typename _Safe = is_nothrow_move_constructible<_Tp>,
111 bool _Fits = (sizeof(_Tp) <= sizeof(_Storage))
112 && (alignof(_Tp) <= alignof(_Storage))>
113 using _Internal = std::integral_constant<bool, _Safe::value && _Fits>;
115 template<typename _Tp>
116 struct _Manager_internal; // uses small-object optimization
118 template<typename _Tp>
119 struct _Manager_external; // creates contained object on the heap
121 template<typename _Tp>
122 using _Manager = __conditional_t<_Internal<_Tp>::value,
123 _Manager_internal<_Tp>,
124 _Manager_external<_Tp>>;
126 template<typename _Tp, typename _Decayed = decay_t<_Tp>>
127 using _Decay = enable_if_t<!is_same<_Decayed, any>::value, _Decayed>;
130 // construct/destruct
132 /// Default constructor, creates an empty object.
133 any() noexcept : _M_manager(nullptr) { }
135 /// Copy constructor, copies the state of @p __other
136 any(const any& __other)
139 _M_manager = nullptr;
144 __other._M_manager(_Op_clone, &__other, &__arg);
149 * @brief Move constructor, transfer the state from @p __other
151 * @post @c __other.empty() (this postcondition is a GNU extension)
153 any(any&& __other) noexcept
156 _M_manager = nullptr;
161 __other._M_manager(_Op_xfer, &__other, &__arg);
165 /// Construct with a copy of @p __value as the contained object.
166 template <typename _ValueType, typename _Tp = _Decay<_ValueType>,
167 typename _Mgr = _Manager<_Tp>,
168 typename enable_if<is_constructible<_Tp, _ValueType&&>::value,
170 any(_ValueType&& __value)
171 : _M_manager(&_Mgr::_S_manage)
173 _Mgr::_S_create(_M_storage, std::forward<_ValueType>(__value));
174 static_assert(is_copy_constructible<_Tp>::value,
175 "The contained object must be CopyConstructible");
178 /// Construct with a copy of @p __value as the contained object.
179 template <typename _ValueType, typename _Tp = _Decay<_ValueType>,
180 typename _Mgr = _Manager<_Tp>,
181 typename enable_if<!is_constructible<_Tp, _ValueType&&>::value,
183 any(_ValueType&& __value)
184 : _M_manager(&_Mgr::_S_manage)
186 _Mgr::_S_create(_M_storage, __value);
187 static_assert(is_copy_constructible<_Tp>::value,
188 "The contained object must be CopyConstructible");
191 /// Destructor, calls @c clear()
196 /// Copy the state of another object.
197 any& operator=(const any& __rhs)
204 * @brief Move assignment operator
206 * @post @c __rhs.empty() (not guaranteed for other implementations)
208 any& operator=(any&& __rhs) noexcept
212 else if (this != &__rhs)
217 __rhs._M_manager(_Op_xfer, &__rhs, &__arg);
222 /// Store a copy of @p __rhs as the contained object.
223 template<typename _ValueType>
224 enable_if_t<!is_same<any, decay_t<_ValueType>>::value, any&>
225 operator=(_ValueType&& __rhs)
227 *this = any(std::forward<_ValueType>(__rhs));
233 /// If not empty, destroy the contained object.
234 void clear() noexcept
238 _M_manager(_Op_destroy, this, nullptr);
239 _M_manager = nullptr;
243 /// Exchange state with another object.
244 void swap(any& __rhs) noexcept
246 if (empty() && __rhs.empty())
249 if (!empty() && !__rhs.empty())
256 __arg._M_any = &__tmp;
257 __rhs._M_manager(_Op_xfer, &__rhs, &__arg);
258 __arg._M_any = &__rhs;
259 _M_manager(_Op_xfer, this, &__arg);
261 __tmp._M_manager(_Op_xfer, &__tmp, &__arg);
265 any* __empty = empty() ? this : &__rhs;
266 any* __full = empty() ? &__rhs : this;
268 __arg._M_any = __empty;
269 __full->_M_manager(_Op_xfer, __full, &__arg);
275 /// Reports whether there is a contained object or not.
276 _GLIBCXX_NODISCARD bool empty() const noexcept { return _M_manager == nullptr; }
279 /// The @c typeid of the contained object, or @c typeid(void) if empty.
280 const type_info& type() const noexcept
285 _M_manager(_Op_get_type_info, this, &__arg);
286 return *__arg._M_typeinfo;
290 template<typename _Tp>
291 static constexpr bool __is_valid_cast()
292 { return __or_<is_reference<_Tp>, is_copy_constructible<_Tp>>::value; }
296 _Op_access, _Op_get_type_info, _Op_clone, _Op_destroy, _Op_xfer
302 const std::type_info* _M_typeinfo;
306 void (*_M_manager)(_Op, const any*, _Arg*);
309 template<typename _Tp>
310 friend enable_if_t<is_object<_Tp>::value, void*>
311 __any_caster(const any* __any);
313 // Manage in-place contained object.
314 template<typename _Tp>
315 struct _Manager_internal
318 _S_manage(_Op __which, const any* __anyp, _Arg* __arg);
320 template<typename _Up>
322 _S_create(_Storage& __storage, _Up&& __value)
324 void* __addr = &__storage._M_buffer;
325 ::new (__addr) _Tp(std::forward<_Up>(__value));
329 // Manage external contained object.
330 template<typename _Tp>
331 struct _Manager_external
334 _S_manage(_Op __which, const any* __anyp, _Arg* __arg);
336 template<typename _Up>
338 _S_create(_Storage& __storage, _Up&& __value)
340 __storage._M_ptr = new _Tp(std::forward<_Up>(__value));
345 /// Exchange the states of two @c any objects.
346 inline void swap(any& __x, any& __y) noexcept { __x.swap(__y); }
349 * @brief Access the contained object.
351 * @tparam _ValueType A const-reference or CopyConstructible type.
352 * @param __any The object to access.
353 * @return The contained object.
354 * @throw bad_any_cast If <code>
355 * __any.type() != typeid(remove_reference_t<_ValueType>)
358 template<typename _ValueType>
359 inline _ValueType any_cast(const any& __any)
361 static_assert(any::__is_valid_cast<_ValueType>(),
362 "Template argument must be a reference or CopyConstructible type");
363 auto __p = any_cast<add_const_t<remove_reference_t<_ValueType>>>(&__any);
366 __throw_bad_any_cast();
370 * @brief Access the contained object.
372 * @tparam _ValueType A reference or CopyConstructible type.
373 * @param __any The object to access.
374 * @return The contained object.
375 * @throw bad_any_cast If <code>
376 * __any.type() != typeid(remove_reference_t<_ValueType>)
381 template<typename _ValueType>
382 inline _ValueType any_cast(any& __any)
384 static_assert(any::__is_valid_cast<_ValueType>(),
385 "Template argument must be a reference or CopyConstructible type");
386 auto __p = any_cast<remove_reference_t<_ValueType>>(&__any);
389 __throw_bad_any_cast();
392 template<typename _ValueType,
393 typename enable_if<!is_move_constructible<_ValueType>::value
394 || is_lvalue_reference<_ValueType>::value,
396 inline _ValueType any_cast(any&& __any)
398 static_assert(any::__is_valid_cast<_ValueType>(),
399 "Template argument must be a reference or CopyConstructible type");
400 auto __p = any_cast<remove_reference_t<_ValueType>>(&__any);
403 __throw_bad_any_cast();
406 template<typename _ValueType,
407 typename enable_if<is_move_constructible<_ValueType>::value
408 && !is_lvalue_reference<_ValueType>::value,
410 inline _ValueType any_cast(any&& __any)
412 static_assert(any::__is_valid_cast<_ValueType>(),
413 "Template argument must be a reference or CopyConstructible type");
414 auto __p = any_cast<remove_reference_t<_ValueType>>(&__any);
416 return std::move(*__p);
417 __throw_bad_any_cast();
421 /// @cond undocumented
422 template<typename _Tp>
423 enable_if_t<is_object<_Tp>::value, void*>
424 __any_caster(const any* __any)
426 // any_cast<T> returns non-null if __any->type() == typeid(T) and
427 // typeid(T) ignores cv-qualifiers so remove them:
428 using _Up = remove_cv_t<_Tp>;
429 // The contained value has a decayed type, so if decay_t<U> is not U,
430 // then it's not possible to have a contained value of type U.
431 using __does_not_decay = is_same<decay_t<_Up>, _Up>;
432 // Only copy constructible types can be used for contained values.
433 using __is_copyable = is_copy_constructible<_Up>;
434 // If the type _Tp could never be stored in an any we don't want to
435 // instantiate _Manager<_Tp>, so use _Manager<any::_Op> instead, which
436 // is explicitly specialized and has a no-op _S_manage function.
437 using _Vp = __conditional_t<__and_<__does_not_decay, __is_copyable>{},
439 // First try comparing function addresses, which works without RTTI
440 if (__any->_M_manager == &any::_Manager<_Vp>::_S_manage
442 || __any->type() == typeid(_Tp)
447 __any->_M_manager(any::_Op_access, __any, &__arg);
453 // This overload exists so that std::any_cast<void(*)()>(a) is well-formed.
454 template<typename _Tp>
455 enable_if_t<!is_object<_Tp>::value, _Tp*>
456 __any_caster(const any*) noexcept
461 * @brief Access the contained object.
463 * @tparam _ValueType The type of the contained object.
464 * @param __any A pointer to the object to access.
465 * @return The address of the contained object if <code>
466 * __any != nullptr && __any.type() == typeid(_ValueType)
467 * </code>, otherwise a null pointer.
471 template<typename _ValueType>
472 inline const _ValueType* any_cast(const any* __any) noexcept
475 return static_cast<_ValueType*>(__any_caster<_ValueType>(__any));
479 template<typename _ValueType>
480 inline _ValueType* any_cast(any* __any) noexcept
483 return static_cast<_ValueType*>(__any_caster<_ValueType>(__any));
488 template<typename _Tp>
490 any::_Manager_internal<_Tp>::
491 _S_manage(_Op __which, const any* __any, _Arg* __arg)
493 // The contained object is in _M_storage._M_buffer
494 auto __ptr = reinterpret_cast<const _Tp*>(&__any->_M_storage._M_buffer);
498 __arg->_M_obj = const_cast<_Tp*>(__ptr);
500 case _Op_get_type_info:
502 __arg->_M_typeinfo = &typeid(_Tp);
506 ::new(&__arg->_M_any->_M_storage._M_buffer) _Tp(*__ptr);
507 __arg->_M_any->_M_manager = __any->_M_manager;
513 ::new(&__arg->_M_any->_M_storage._M_buffer) _Tp
514 (std::move(*const_cast<_Tp*>(__ptr)));
516 __arg->_M_any->_M_manager = __any->_M_manager;
517 const_cast<any*>(__any)->_M_manager = nullptr;
522 template<typename _Tp>
524 any::_Manager_external<_Tp>::
525 _S_manage(_Op __which, const any* __any, _Arg* __arg)
527 // The contained object is *_M_storage._M_ptr
528 auto __ptr = static_cast<const _Tp*>(__any->_M_storage._M_ptr);
532 __arg->_M_obj = const_cast<_Tp*>(__ptr);
534 case _Op_get_type_info:
536 __arg->_M_typeinfo = &typeid(_Tp);
540 __arg->_M_any->_M_storage._M_ptr = new _Tp(*__ptr);
541 __arg->_M_any->_M_manager = __any->_M_manager;
547 __arg->_M_any->_M_storage._M_ptr = __any->_M_storage._M_ptr;
548 __arg->_M_any->_M_manager = __any->_M_manager;
549 const_cast<any*>(__any)->_M_manager = nullptr;
554 // Dummy specialization used by __any_caster.
556 struct any::_Manager_internal<any::_Op>
559 _S_manage(_Op, const any*, _Arg*) { }
563} // namespace fundamentals_v1
564} // namespace experimental
566_GLIBCXX_END_NAMESPACE_VERSION
571#endif // _GLIBCXX_EXPERIMENTAL_ANY