• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// -*- C++ -*-
2//===----------------------------------------------------------------------===//
3//
4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_ANY
11#define _LIBCPP_ANY
12
13/*
14   any synopsis
15
16namespace std {
17
18  class bad_any_cast : public bad_cast
19  {
20  public:
21    virtual const char* what() const noexcept;
22  };
23
24  class any
25  {
26  public:
27
28    // 6.3.1 any construct/destruct
29    any() noexcept;
30
31    any(const any& other);
32    any(any&& other) noexcept;
33
34    template <class ValueType>
35      any(ValueType&& value);
36
37    ~any();
38
39    // 6.3.2 any assignments
40    any& operator=(const any& rhs);
41    any& operator=(any&& rhs) noexcept;
42
43    template <class ValueType>
44      any& operator=(ValueType&& rhs);
45
46    // 6.3.3 any modifiers
47    template <class ValueType, class... Args>
48      decay_t<ValueType>& emplace(Args&&... args);
49    template <class ValueType, class U, class... Args>
50      decay_t<ValueType>& emplace(initializer_list<U>, Args&&...);
51    void reset() noexcept;
52    void swap(any& rhs) noexcept;
53
54    // 6.3.4 any observers
55    bool has_value() const noexcept;
56    const type_info& type() const noexcept;
57  };
58
59   // 6.4 Non-member functions
60  void swap(any& x, any& y) noexcept;
61
62  template <class T, class ...Args>
63    any make_any(Args&& ...args);
64  template <class T, class U, class ...Args>
65    any make_any(initializer_list<U>, Args&& ...args);
66
67  template<class ValueType>
68    ValueType any_cast(const any& operand);
69  template<class ValueType>
70    ValueType any_cast(any& operand);
71  template<class ValueType>
72    ValueType any_cast(any&& operand);
73
74  template<class ValueType>
75    const ValueType* any_cast(const any* operand) noexcept;
76  template<class ValueType>
77    ValueType* any_cast(any* operand) noexcept;
78
79} // namespace std
80
81*/
82
83#include <__assert> // all public C++ headers provide the assertion handler
84#include <__availability>
85#include <__config>
86#include <__memory/allocator.h>
87#include <__memory/allocator_destructor.h>
88#include <__memory/allocator_traits.h>
89#include <__memory/unique_ptr.h>
90#include <__type_traits/add_const.h>
91#include <__type_traits/add_pointer.h>
92#include <__type_traits/aligned_storage.h>
93#include <__type_traits/alignment_of.h>
94#include <__type_traits/conditional.h>
95#include <__type_traits/decay.h>
96#include <__type_traits/is_constructible.h>
97#include <__type_traits/is_copy_constructible.h>
98#include <__type_traits/is_function.h>
99#include <__type_traits/is_nothrow_move_constructible.h>
100#include <__type_traits/is_reference.h>
101#include <__type_traits/is_same.h>
102#include <__type_traits/remove_cv.h>
103#include <__type_traits/remove_cvref.h>
104#include <__type_traits/remove_reference.h>
105#include <__utility/forward.h>
106#include <__utility/in_place.h>
107#include <__utility/move.h>
108#include <__utility/unreachable.h>
109#include <__verbose_abort>
110#include <initializer_list>
111#include <typeinfo>
112#include <version>
113
114#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
115#  pragma GCC system_header
116#endif
117
118namespace std {
119class _LIBCPP_EXCEPTION_ABI _LIBCPP_AVAILABILITY_BAD_ANY_CAST bad_any_cast : public bad_cast
120{
121public:
122    const char* what() const _NOEXCEPT override;
123};
124} // namespace std
125
126_LIBCPP_BEGIN_NAMESPACE_STD
127
128#if _LIBCPP_STD_VER >= 17
129
130_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
131_LIBCPP_AVAILABILITY_THROW_BAD_ANY_CAST
132void __throw_bad_any_cast()
133{
134#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
135    throw bad_any_cast();
136#else
137    _LIBCPP_VERBOSE_ABORT("bad_any_cast was thrown in -fno-exceptions mode");
138#endif
139}
140
141// Forward declarations
142class _LIBCPP_TEMPLATE_VIS any;
143
144template <class _ValueType>
145_LIBCPP_INLINE_VISIBILITY
146add_pointer_t<add_const_t<_ValueType>>
147any_cast(any const *) _NOEXCEPT;
148
149template <class _ValueType>
150_LIBCPP_INLINE_VISIBILITY
151add_pointer_t<_ValueType> any_cast(any *) _NOEXCEPT;
152
153namespace __any_imp
154{
155  _LIBCPP_SUPPRESS_DEPRECATED_PUSH
156  using _Buffer = aligned_storage_t<3*sizeof(void*), alignment_of<void*>::value>;
157  _LIBCPP_SUPPRESS_DEPRECATED_POP
158
159  template <class _Tp>
160  using _IsSmallObject = integral_constant<bool
161        , sizeof(_Tp) <= sizeof(_Buffer)
162          && alignment_of<_Buffer>::value
163             % alignment_of<_Tp>::value == 0
164          && is_nothrow_move_constructible<_Tp>::value
165        >;
166
167  enum class _Action {
168    _Destroy,
169    _Copy,
170    _Move,
171    _Get,
172    _TypeInfo
173  };
174
175  template <class _Tp> struct _SmallHandler;
176  template <class _Tp> struct _LargeHandler;
177
178  template <class _Tp>
179  struct  _LIBCPP_TEMPLATE_VIS __unique_typeinfo { static constexpr int __id = 0; };
180  template <class _Tp> constexpr int __unique_typeinfo<_Tp>::__id;
181
182  template <class _Tp>
183  inline _LIBCPP_INLINE_VISIBILITY
184  constexpr const void* __get_fallback_typeid() {
185      return &__unique_typeinfo<remove_cv_t<remove_reference_t<_Tp>>>::__id;
186  }
187
188  template <class _Tp>
189  inline _LIBCPP_INLINE_VISIBILITY
190  bool __compare_typeid(type_info const* __id, const void* __fallback_id)
191  {
192#if !defined(_LIBCPP_HAS_NO_RTTI)
193      if (__id && *__id == typeid(_Tp))
194          return true;
195#endif
196      if (!__id && __fallback_id == __any_imp::__get_fallback_typeid<_Tp>())
197          return true;
198      return false;
199  }
200
201  template <class _Tp>
202  using _Handler = conditional_t<
203    _IsSmallObject<_Tp>::value, _SmallHandler<_Tp>, _LargeHandler<_Tp>>;
204
205} // namespace __any_imp
206
207class _LIBCPP_TEMPLATE_VIS any
208{
209public:
210  // construct/destruct
211  _LIBCPP_INLINE_VISIBILITY
212  constexpr any() _NOEXCEPT : __h_(nullptr) {}
213
214  _LIBCPP_INLINE_VISIBILITY
215  any(any const & __other) : __h_(nullptr)
216  {
217    if (__other.__h_) __other.__call(_Action::_Copy, this);
218  }
219
220  _LIBCPP_INLINE_VISIBILITY
221  any(any && __other) _NOEXCEPT : __h_(nullptr)
222  {
223    if (__other.__h_) __other.__call(_Action::_Move, this);
224  }
225
226  template <
227      class _ValueType
228    , class _Tp = decay_t<_ValueType>
229    , class = enable_if_t<
230        !is_same<_Tp, any>::value &&
231        !__is_inplace_type<_ValueType>::value &&
232        is_copy_constructible<_Tp>::value>
233    >
234  _LIBCPP_INLINE_VISIBILITY
235  any(_ValueType && __value);
236
237  template <class _ValueType, class ..._Args,
238    class _Tp = decay_t<_ValueType>,
239    class = enable_if_t<
240        is_constructible<_Tp, _Args...>::value &&
241        is_copy_constructible<_Tp>::value
242    >
243  >
244  _LIBCPP_INLINE_VISIBILITY
245  explicit any(in_place_type_t<_ValueType>, _Args&&... __args);
246
247  template <class _ValueType, class _Up, class ..._Args,
248    class _Tp = decay_t<_ValueType>,
249    class = enable_if_t<
250        is_constructible<_Tp, initializer_list<_Up>&, _Args...>::value &&
251        is_copy_constructible<_Tp>::value>
252  >
253  _LIBCPP_INLINE_VISIBILITY
254  explicit any(in_place_type_t<_ValueType>, initializer_list<_Up>, _Args&&... __args);
255
256  _LIBCPP_INLINE_VISIBILITY
257  ~any() { this->reset(); }
258
259  // assignments
260  _LIBCPP_INLINE_VISIBILITY
261  any & operator=(any const & __rhs) {
262    any(__rhs).swap(*this);
263    return *this;
264  }
265
266  _LIBCPP_INLINE_VISIBILITY
267  any & operator=(any && __rhs) _NOEXCEPT {
268    any(_VSTD::move(__rhs)).swap(*this);
269    return *this;
270  }
271
272  template <
273      class _ValueType
274    , class _Tp = decay_t<_ValueType>
275    , class = enable_if_t<
276          !is_same<_Tp, any>::value
277          && is_copy_constructible<_Tp>::value>
278    >
279  _LIBCPP_INLINE_VISIBILITY
280  any & operator=(_ValueType && __rhs);
281
282  template <class _ValueType, class ..._Args,
283    class _Tp = decay_t<_ValueType>,
284    class = enable_if_t<
285        is_constructible<_Tp, _Args...>::value &&
286        is_copy_constructible<_Tp>::value>
287    >
288  _LIBCPP_INLINE_VISIBILITY
289  _Tp& emplace(_Args&&...);
290
291  template <class _ValueType, class _Up, class ..._Args,
292    class _Tp = decay_t<_ValueType>,
293    class = enable_if_t<
294        is_constructible<_Tp, initializer_list<_Up>&, _Args...>::value &&
295        is_copy_constructible<_Tp>::value>
296  >
297  _LIBCPP_INLINE_VISIBILITY
298  _Tp& emplace(initializer_list<_Up>, _Args&&...);
299
300  // 6.3.3 any modifiers
301  _LIBCPP_INLINE_VISIBILITY
302  void reset() _NOEXCEPT { if (__h_) this->__call(_Action::_Destroy); }
303
304  _LIBCPP_INLINE_VISIBILITY
305  void swap(any & __rhs) _NOEXCEPT;
306
307  // 6.3.4 any observers
308  _LIBCPP_INLINE_VISIBILITY
309  bool has_value() const _NOEXCEPT { return __h_ != nullptr; }
310
311#if !defined(_LIBCPP_HAS_NO_RTTI)
312  _LIBCPP_INLINE_VISIBILITY
313  const type_info & type() const _NOEXCEPT {
314    if (__h_) {
315        return *static_cast<type_info const *>(this->__call(_Action::_TypeInfo));
316    } else {
317        return typeid(void);
318    }
319  }
320#endif
321
322private:
323    typedef __any_imp::_Action _Action;
324    using _HandleFuncPtr =  void* (*)(_Action, any const *, any *, const type_info *,
325      const void* __fallback_info);
326
327    union _Storage {
328        _LIBCPP_HIDE_FROM_ABI constexpr _Storage() : __ptr(nullptr) {}
329        void *  __ptr;
330        __any_imp::_Buffer __buf;
331    };
332
333    _LIBCPP_INLINE_VISIBILITY
334    void * __call(_Action __a, any * __other = nullptr,
335                  type_info const * __info = nullptr,
336                   const void* __fallback_info = nullptr) const
337    {
338        return __h_(__a, this, __other, __info, __fallback_info);
339    }
340
341    _LIBCPP_INLINE_VISIBILITY
342    void * __call(_Action __a, any * __other = nullptr,
343                  type_info const * __info = nullptr,
344                  const void* __fallback_info = nullptr)
345    {
346        return __h_(__a, this, __other, __info, __fallback_info);
347    }
348
349    template <class>
350    friend struct __any_imp::_SmallHandler;
351    template <class>
352    friend struct __any_imp::_LargeHandler;
353
354    template <class _ValueType>
355    friend add_pointer_t<add_const_t<_ValueType>>
356    any_cast(any const *) _NOEXCEPT;
357
358    template <class _ValueType>
359    friend add_pointer_t<_ValueType>
360    any_cast(any *) _NOEXCEPT;
361
362    _HandleFuncPtr __h_ = nullptr;
363    _Storage __s_;
364};
365
366namespace __any_imp
367{
368  template <class _Tp>
369  struct _LIBCPP_TEMPLATE_VIS _SmallHandler
370  {
371     _LIBCPP_INLINE_VISIBILITY
372     static void* __handle(_Action __act, any const * __this, any * __other,
373                           type_info const * __info, const void* __fallback_info)
374     {
375        switch (__act)
376        {
377        case _Action::_Destroy:
378          __destroy(const_cast<any &>(*__this));
379          return nullptr;
380        case _Action::_Copy:
381            __copy(*__this, *__other);
382            return nullptr;
383        case _Action::_Move:
384          __move(const_cast<any &>(*__this), *__other);
385          return nullptr;
386        case _Action::_Get:
387            return __get(const_cast<any &>(*__this), __info, __fallback_info);
388        case _Action::_TypeInfo:
389          return __type_info();
390        }
391        __libcpp_unreachable();
392    }
393
394    template <class ..._Args>
395    _LIBCPP_INLINE_VISIBILITY
396    static _Tp& __create(any & __dest, _Args&&... __args) {
397        typedef allocator<_Tp> _Alloc;
398        typedef allocator_traits<_Alloc> _ATraits;
399        _Alloc __a;
400        _Tp * __ret = static_cast<_Tp*>(static_cast<void*>(&__dest.__s_.__buf));
401        _ATraits::construct(__a, __ret, _VSTD::forward<_Args>(__args)...);
402        __dest.__h_ = &_SmallHandler::__handle;
403        return *__ret;
404    }
405
406  private:
407    _LIBCPP_INLINE_VISIBILITY
408    static void __destroy(any & __this) {
409        typedef allocator<_Tp> _Alloc;
410        typedef allocator_traits<_Alloc> _ATraits;
411        _Alloc __a;
412        _Tp * __p = static_cast<_Tp *>(static_cast<void*>(&__this.__s_.__buf));
413        _ATraits::destroy(__a, __p);
414        __this.__h_ = nullptr;
415    }
416
417    _LIBCPP_INLINE_VISIBILITY
418    static void __copy(any const & __this, any & __dest) {
419        _SmallHandler::__create(__dest, *static_cast<_Tp const *>(
420            static_cast<void const *>(&__this.__s_.__buf)));
421    }
422
423    _LIBCPP_INLINE_VISIBILITY
424    static void __move(any & __this, any & __dest) {
425        _SmallHandler::__create(__dest, _VSTD::move(
426            *static_cast<_Tp*>(static_cast<void*>(&__this.__s_.__buf))));
427        __destroy(__this);
428    }
429
430    _LIBCPP_INLINE_VISIBILITY
431    static void* __get(any & __this,
432                       type_info const * __info,
433                       const void* __fallback_id)
434    {
435        if (__any_imp::__compare_typeid<_Tp>(__info, __fallback_id))
436            return static_cast<void*>(&__this.__s_.__buf);
437        return nullptr;
438    }
439
440    _LIBCPP_INLINE_VISIBILITY
441    static void* __type_info()
442    {
443#if !defined(_LIBCPP_HAS_NO_RTTI)
444        return const_cast<void*>(static_cast<void const *>(&typeid(_Tp)));
445#else
446        return nullptr;
447#endif
448    }
449  };
450
451  template <class _Tp>
452  struct _LIBCPP_TEMPLATE_VIS _LargeHandler
453  {
454    _LIBCPP_INLINE_VISIBILITY
455    static void* __handle(_Action __act, any const * __this,
456                          any * __other, type_info const * __info,
457                          void const* __fallback_info)
458    {
459        switch (__act)
460        {
461        case _Action::_Destroy:
462          __destroy(const_cast<any &>(*__this));
463          return nullptr;
464        case _Action::_Copy:
465          __copy(*__this, *__other);
466          return nullptr;
467        case _Action::_Move:
468          __move(const_cast<any &>(*__this), *__other);
469          return nullptr;
470        case _Action::_Get:
471            return __get(const_cast<any &>(*__this), __info, __fallback_info);
472        case _Action::_TypeInfo:
473          return __type_info();
474        }
475        __libcpp_unreachable();
476    }
477
478    template <class ..._Args>
479    _LIBCPP_INLINE_VISIBILITY
480    static _Tp& __create(any & __dest, _Args&&... __args) {
481        typedef allocator<_Tp> _Alloc;
482        typedef allocator_traits<_Alloc> _ATraits;
483        typedef __allocator_destructor<_Alloc> _Dp;
484        _Alloc __a;
485        unique_ptr<_Tp, _Dp> __hold(_ATraits::allocate(__a, 1), _Dp(__a, 1));
486        _Tp * __ret = __hold.get();
487        _ATraits::construct(__a, __ret, _VSTD::forward<_Args>(__args)...);
488        __dest.__s_.__ptr = __hold.release();
489        __dest.__h_ = &_LargeHandler::__handle;
490        return *__ret;
491    }
492
493  private:
494
495    _LIBCPP_INLINE_VISIBILITY
496    static void __destroy(any & __this){
497        typedef allocator<_Tp> _Alloc;
498        typedef allocator_traits<_Alloc> _ATraits;
499        _Alloc __a;
500        _Tp * __p = static_cast<_Tp *>(__this.__s_.__ptr);
501        _ATraits::destroy(__a, __p);
502        _ATraits::deallocate(__a, __p, 1);
503        __this.__h_ = nullptr;
504    }
505
506    _LIBCPP_INLINE_VISIBILITY
507    static void __copy(any const & __this, any & __dest) {
508        _LargeHandler::__create(__dest, *static_cast<_Tp const *>(__this.__s_.__ptr));
509    }
510
511    _LIBCPP_INLINE_VISIBILITY
512    static void __move(any & __this, any & __dest) {
513      __dest.__s_.__ptr = __this.__s_.__ptr;
514      __dest.__h_ = &_LargeHandler::__handle;
515      __this.__h_ = nullptr;
516    }
517
518    _LIBCPP_INLINE_VISIBILITY
519    static void* __get(any & __this, type_info const * __info,
520                       void const* __fallback_info)
521    {
522        if (__any_imp::__compare_typeid<_Tp>(__info, __fallback_info))
523            return static_cast<void*>(__this.__s_.__ptr);
524        return nullptr;
525
526    }
527
528    _LIBCPP_INLINE_VISIBILITY
529    static void* __type_info()
530    {
531#if !defined(_LIBCPP_HAS_NO_RTTI)
532        return const_cast<void*>(static_cast<void const *>(&typeid(_Tp)));
533#else
534        return nullptr;
535#endif
536    }
537  };
538
539} // namespace __any_imp
540
541
542template <class _ValueType, class _Tp, class>
543any::any(_ValueType && __v) : __h_(nullptr)
544{
545  __any_imp::_Handler<_Tp>::__create(*this, _VSTD::forward<_ValueType>(__v));
546}
547
548template <class _ValueType, class ..._Args, class _Tp, class>
549any::any(in_place_type_t<_ValueType>, _Args&&... __args) {
550  __any_imp::_Handler<_Tp>::__create(*this, _VSTD::forward<_Args>(__args)...);
551}
552
553template <class _ValueType, class _Up, class ..._Args, class _Tp, class>
554any::any(in_place_type_t<_ValueType>, initializer_list<_Up> __il, _Args&&... __args) {
555  __any_imp::_Handler<_Tp>::__create(*this, __il, _VSTD::forward<_Args>(__args)...);
556}
557
558template <class _ValueType, class, class>
559inline _LIBCPP_INLINE_VISIBILITY
560any & any::operator=(_ValueType && __v)
561{
562  any(_VSTD::forward<_ValueType>(__v)).swap(*this);
563  return *this;
564}
565
566template <class _ValueType, class ..._Args, class _Tp, class>
567inline _LIBCPP_INLINE_VISIBILITY
568_Tp& any::emplace(_Args&&... __args) {
569  reset();
570  return __any_imp::_Handler<_Tp>::__create(*this, _VSTD::forward<_Args>(__args)...);
571}
572
573template <class _ValueType, class _Up, class ..._Args, class _Tp, class>
574inline _LIBCPP_INLINE_VISIBILITY
575_Tp& any::emplace(initializer_list<_Up> __il, _Args&&... __args) {
576  reset();
577  return __any_imp::_Handler<_Tp>::__create(*this, __il, _VSTD::forward<_Args>(__args)...);
578}
579
580inline _LIBCPP_INLINE_VISIBILITY
581void any::swap(any & __rhs) _NOEXCEPT
582{
583    if (this == &__rhs)
584      return;
585    if (__h_ && __rhs.__h_) {
586        any __tmp;
587        __rhs.__call(_Action::_Move, &__tmp);
588        this->__call(_Action::_Move, &__rhs);
589        __tmp.__call(_Action::_Move, this);
590    }
591    else if (__h_) {
592        this->__call(_Action::_Move, &__rhs);
593    }
594    else if (__rhs.__h_) {
595        __rhs.__call(_Action::_Move, this);
596    }
597}
598
599// 6.4 Non-member functions
600
601inline _LIBCPP_INLINE_VISIBILITY
602void swap(any & __lhs, any & __rhs) _NOEXCEPT
603{
604    __lhs.swap(__rhs);
605}
606
607template <class _Tp, class ..._Args>
608inline _LIBCPP_INLINE_VISIBILITY
609any make_any(_Args&&... __args) {
610    return any(in_place_type<_Tp>, _VSTD::forward<_Args>(__args)...);
611}
612
613template <class _Tp, class _Up, class ..._Args>
614inline _LIBCPP_INLINE_VISIBILITY
615any make_any(initializer_list<_Up> __il, _Args&&... __args) {
616    return any(in_place_type<_Tp>, __il, _VSTD::forward<_Args>(__args)...);
617}
618
619template <class _ValueType>
620inline _LIBCPP_INLINE_VISIBILITY
621_LIBCPP_AVAILABILITY_THROW_BAD_ANY_CAST
622_ValueType any_cast(any const & __v)
623{
624    using _RawValueType = __remove_cvref_t<_ValueType>;
625    static_assert(is_constructible<_ValueType, _RawValueType const &>::value,
626                  "ValueType is required to be a const lvalue reference "
627                  "or a CopyConstructible type");
628    auto __tmp = _VSTD::any_cast<add_const_t<_RawValueType>>(&__v);
629    if (__tmp == nullptr)
630        __throw_bad_any_cast();
631    return static_cast<_ValueType>(*__tmp);
632}
633
634template <class _ValueType>
635inline _LIBCPP_INLINE_VISIBILITY
636_LIBCPP_AVAILABILITY_THROW_BAD_ANY_CAST
637_ValueType any_cast(any & __v)
638{
639    using _RawValueType = __remove_cvref_t<_ValueType>;
640    static_assert(is_constructible<_ValueType, _RawValueType &>::value,
641                  "ValueType is required to be an lvalue reference "
642                  "or a CopyConstructible type");
643    auto __tmp = _VSTD::any_cast<_RawValueType>(&__v);
644    if (__tmp == nullptr)
645        __throw_bad_any_cast();
646    return static_cast<_ValueType>(*__tmp);
647}
648
649template <class _ValueType>
650inline _LIBCPP_INLINE_VISIBILITY
651_LIBCPP_AVAILABILITY_THROW_BAD_ANY_CAST
652_ValueType any_cast(any && __v)
653{
654    using _RawValueType = __remove_cvref_t<_ValueType>;
655    static_assert(is_constructible<_ValueType, _RawValueType>::value,
656                  "ValueType is required to be an rvalue reference "
657                  "or a CopyConstructible type");
658    auto __tmp = _VSTD::any_cast<_RawValueType>(&__v);
659    if (__tmp == nullptr)
660        __throw_bad_any_cast();
661    return static_cast<_ValueType>(_VSTD::move(*__tmp));
662}
663
664template <class _ValueType>
665inline _LIBCPP_INLINE_VISIBILITY
666add_pointer_t<add_const_t<_ValueType>>
667any_cast(any const * __any) _NOEXCEPT
668{
669    static_assert(!is_reference<_ValueType>::value,
670                  "_ValueType may not be a reference.");
671    return _VSTD::any_cast<_ValueType>(const_cast<any *>(__any));
672}
673
674template <class _RetType>
675inline _LIBCPP_INLINE_VISIBILITY
676_RetType __pointer_or_func_cast(void* __p, /*IsFunction*/false_type) noexcept {
677  return static_cast<_RetType>(__p);
678}
679
680template <class _RetType>
681inline _LIBCPP_INLINE_VISIBILITY
682_RetType __pointer_or_func_cast(void*, /*IsFunction*/true_type) noexcept {
683  return nullptr;
684}
685
686template <class _ValueType>
687_LIBCPP_HIDE_FROM_ABI
688add_pointer_t<_ValueType>
689any_cast(any * __any) _NOEXCEPT
690{
691    using __any_imp::_Action;
692    static_assert(!is_reference<_ValueType>::value,
693                  "_ValueType may not be a reference.");
694    typedef add_pointer_t<_ValueType> _ReturnType;
695    if (__any && __any->__h_) {
696      void *__p = __any->__call(_Action::_Get, nullptr,
697#if !defined(_LIBCPP_HAS_NO_RTTI)
698                          &typeid(_ValueType),
699#else
700                          nullptr,
701#endif
702                          __any_imp::__get_fallback_typeid<_ValueType>());
703        return _VSTD::__pointer_or_func_cast<_ReturnType>(
704            __p, is_function<_ValueType>{});
705    }
706    return nullptr;
707}
708
709#endif // _LIBCPP_STD_VER >= 17
710
711_LIBCPP_END_NAMESPACE_STD
712
713#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 17
714#  include <chrono>
715#endif
716
717#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
718#  include <atomic>
719#  include <concepts>
720#  include <cstdlib>
721#  include <iosfwd>
722#  include <iterator>
723#  include <memory>
724#  include <type_traits>
725#  include <variant>
726#endif
727
728#endif // _LIBCPP_ANY
729