Path: blob/main/contrib/llvm-project/libcxx/include/__utility/no_destroy.h
35235 views
//===----------------------------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#ifndef _LIBCPP___UTILITY_NO_DESTROY_H9#define _LIBCPP___UTILITY_NO_DESTROY_H1011#include <__config>12#include <__type_traits/is_constant_evaluated.h>13#include <__utility/forward.h>14#include <new>1516#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)17# pragma GCC system_header18#endif1920_LIBCPP_BEGIN_NAMESPACE_STD2122struct __uninitialized_tag {};2324// This class stores an object of type _Tp but never destroys it.25//26// This is akin to using __attribute__((no_destroy)), except that it is possible27// to control the lifetime of the object with more flexibility by deciding e.g.28// whether to initialize the object at construction or to defer to a later29// initialization using __emplace.30template <class _Tp>31struct __no_destroy {32_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR explicit __no_destroy(__uninitialized_tag) : __obj_() {}3334template <class... _Args>35_LIBCPP_HIDE_FROM_ABI explicit __no_destroy(_Args&&... __args) {36::new ((void*)__obj_) _Tp(std::forward<_Args>(__args)...);37}3839template <class... _Args>40_LIBCPP_HIDE_FROM_ABI _Tp& __emplace(_Args&&... __args) {41return *(::new ((void*)__obj_) _Tp(std::forward<_Args>(__args)...));42}4344_LIBCPP_HIDE_FROM_ABI _Tp& __get() { return *reinterpret_cast<_Tp*>(__obj_); }45_LIBCPP_HIDE_FROM_ABI _Tp const& __get() const { return *reinterpret_cast<const _Tp*>(__obj_); }4647private:48_ALIGNAS_TYPE(_Tp) char __obj_[sizeof(_Tp)];49};5051_LIBCPP_END_NAMESPACE_STD5253#endif // _LIBCPP___UTILITY_NO_DESTROY_H545556