Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-03 08:13:45

0001 //===----------------------------------------------------------------------===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef _LIBCPP___CXX03___UTILITY_NO_DESTROY_H
0010 #define _LIBCPP___CXX03___UTILITY_NO_DESTROY_H
0011 
0012 #include <__cxx03/__config>
0013 #include <__cxx03/__type_traits/is_constant_evaluated.h>
0014 #include <__cxx03/__utility/forward.h>
0015 #include <__cxx03/new>
0016 
0017 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0018 #  pragma GCC system_header
0019 #endif
0020 
0021 _LIBCPP_BEGIN_NAMESPACE_STD
0022 
0023 struct __uninitialized_tag {};
0024 
0025 // This class stores an object of type _Tp but never destroys it.
0026 //
0027 // This is akin to using __attribute__((no_destroy)), except that it is possible
0028 // to control the lifetime of the object with more flexibility by deciding e.g.
0029 // whether to initialize the object at construction or to defer to a later
0030 // initialization using __emplace.
0031 template <class _Tp>
0032 struct __no_destroy {
0033   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR explicit __no_destroy(__uninitialized_tag) : __obj_() {}
0034 
0035   template <class... _Args>
0036   _LIBCPP_HIDE_FROM_ABI explicit __no_destroy(_Args&&... __args) {
0037     ::new ((void*)__obj_) _Tp(std::forward<_Args>(__args)...);
0038   }
0039 
0040   template <class... _Args>
0041   _LIBCPP_HIDE_FROM_ABI _Tp& __emplace(_Args&&... __args) {
0042     return *(::new ((void*)__obj_) _Tp(std::forward<_Args>(__args)...));
0043   }
0044 
0045   _LIBCPP_HIDE_FROM_ABI _Tp& __get() { return *reinterpret_cast<_Tp*>(__obj_); }
0046   _LIBCPP_HIDE_FROM_ABI _Tp const& __get() const { return *reinterpret_cast<const _Tp*>(__obj_); }
0047 
0048 private:
0049   _ALIGNAS_TYPE(_Tp) char __obj_[sizeof(_Tp)];
0050 };
0051 
0052 _LIBCPP_END_NAMESPACE_STD
0053 
0054 #endif // _LIBCPP___CXX03___UTILITY_NO_DESTROY_H