Path: blob/main/contrib/llvm-project/libcxx/include/__memory/construct_at.h
35233 views
// -*- C++ -*-1//===----------------------------------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//89#ifndef _LIBCPP___MEMORY_CONSTRUCT_AT_H10#define _LIBCPP___MEMORY_CONSTRUCT_AT_H1112#include <__assert>13#include <__config>14#include <__iterator/access.h>15#include <__memory/addressof.h>16#include <__memory/voidify.h>17#include <__type_traits/enable_if.h>18#include <__type_traits/is_array.h>19#include <__utility/declval.h>20#include <__utility/forward.h>21#include <__utility/move.h>22#include <new>2324#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)25# pragma GCC system_header26#endif2728_LIBCPP_PUSH_MACROS29#include <__undef_macros>3031_LIBCPP_BEGIN_NAMESPACE_STD3233// construct_at3435#if _LIBCPP_STD_VER >= 203637template <class _Tp, class... _Args, class = decltype(::new(std::declval<void*>()) _Tp(std::declval<_Args>()...))>38_LIBCPP_HIDE_FROM_ABI constexpr _Tp* construct_at(_Tp* __location, _Args&&... __args) {39_LIBCPP_ASSERT_NON_NULL(__location != nullptr, "null pointer given to construct_at");40return ::new (std::__voidify(*__location)) _Tp(std::forward<_Args>(__args)...);41}4243#endif4445template <class _Tp, class... _Args, class = decltype(::new(std::declval<void*>()) _Tp(std::declval<_Args>()...))>46_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp* __construct_at(_Tp* __location, _Args&&... __args) {47#if _LIBCPP_STD_VER >= 2048return std::construct_at(__location, std::forward<_Args>(__args)...);49#else50return _LIBCPP_ASSERT_NON_NULL(__location != nullptr, "null pointer given to construct_at"),51::new (std::__voidify(*__location)) _Tp(std::forward<_Args>(__args)...);52#endif53}5455// destroy_at5657// The internal functions are available regardless of the language version (with the exception of the `__destroy_at`58// taking an array).5960template <class _ForwardIterator>61_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _ForwardIterator __destroy(_ForwardIterator, _ForwardIterator);6263template <class _Tp, __enable_if_t<!is_array<_Tp>::value, int> = 0>64_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __destroy_at(_Tp* __loc) {65_LIBCPP_ASSERT_NON_NULL(__loc != nullptr, "null pointer given to destroy_at");66__loc->~_Tp();67}6869#if _LIBCPP_STD_VER >= 2070template <class _Tp, __enable_if_t<is_array<_Tp>::value, int> = 0>71_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __destroy_at(_Tp* __loc) {72_LIBCPP_ASSERT_NON_NULL(__loc != nullptr, "null pointer given to destroy_at");73std::__destroy(std::begin(*__loc), std::end(*__loc));74}75#endif7677template <class _ForwardIterator>78_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _ForwardIterator79__destroy(_ForwardIterator __first, _ForwardIterator __last) {80for (; __first != __last; ++__first)81std::__destroy_at(std::addressof(*__first));82return __first;83}8485template <class _BidirectionalIterator>86_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _BidirectionalIterator87__reverse_destroy(_BidirectionalIterator __first, _BidirectionalIterator __last) {88while (__last != __first) {89--__last;90std::__destroy_at(std::addressof(*__last));91}92return __last;93}9495#if _LIBCPP_STD_VER >= 179697template <class _Tp, enable_if_t<!is_array_v<_Tp>, int> = 0>98_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void destroy_at(_Tp* __loc) {99std::__destroy_at(__loc);100}101102# if _LIBCPP_STD_VER >= 20103template <class _Tp, enable_if_t<is_array_v<_Tp>, int> = 0>104_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void destroy_at(_Tp* __loc) {105std::__destroy_at(__loc);106}107# endif108109template <class _ForwardIterator>110_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void destroy(_ForwardIterator __first, _ForwardIterator __last) {111(void)std::__destroy(std::move(__first), std::move(__last));112}113114template <class _ForwardIterator, class _Size>115_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _ForwardIterator destroy_n(_ForwardIterator __first, _Size __n) {116for (; __n > 0; (void)++__first, --__n)117std::__destroy_at(std::addressof(*__first));118return __first;119}120121#endif // _LIBCPP_STD_VER >= 17122123_LIBCPP_END_NAMESPACE_STD124125_LIBCPP_POP_MACROS126127#endif // _LIBCPP___MEMORY_CONSTRUCT_AT_H128129130