xref: /netbsd-src/external/gpl3/gcc.old/dist/libstdc++-v3/include/bits/stl_construct.h (revision 8feb0f0b7eaff0608f8350bbfa3098827b4bb91b)
11debfc3dSmrg // nonstandard construct and destroy functions -*- C++ -*-
21debfc3dSmrg 
3*8feb0f0bSmrg // Copyright (C) 2001-2020 Free Software Foundation, Inc.
41debfc3dSmrg //
51debfc3dSmrg // This file is part of the GNU ISO C++ Library.  This library is free
61debfc3dSmrg // software; you can redistribute it and/or modify it under the
71debfc3dSmrg // terms of the GNU General Public License as published by the
81debfc3dSmrg // Free Software Foundation; either version 3, or (at your option)
91debfc3dSmrg // any later version.
101debfc3dSmrg 
111debfc3dSmrg // This library is distributed in the hope that it will be useful,
121debfc3dSmrg // but WITHOUT ANY WARRANTY; without even the implied warranty of
131debfc3dSmrg // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
141debfc3dSmrg // GNU General Public License for more details.
151debfc3dSmrg 
161debfc3dSmrg // Under Section 7 of GPL version 3, you are granted additional
171debfc3dSmrg // permissions described in the GCC Runtime Library Exception, version
181debfc3dSmrg // 3.1, as published by the Free Software Foundation.
191debfc3dSmrg 
201debfc3dSmrg // You should have received a copy of the GNU General Public License and
211debfc3dSmrg // a copy of the GCC Runtime Library Exception along with this program;
221debfc3dSmrg // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
231debfc3dSmrg // <http://www.gnu.org/licenses/>.
241debfc3dSmrg 
251debfc3dSmrg /*
261debfc3dSmrg  *
271debfc3dSmrg  * Copyright (c) 1994
281debfc3dSmrg  * Hewlett-Packard Company
291debfc3dSmrg  *
301debfc3dSmrg  * Permission to use, copy, modify, distribute and sell this software
311debfc3dSmrg  * and its documentation for any purpose is hereby granted without fee,
321debfc3dSmrg  * provided that the above copyright notice appear in all copies and
331debfc3dSmrg  * that both that copyright notice and this permission notice appear
341debfc3dSmrg  * in supporting documentation.  Hewlett-Packard Company makes no
351debfc3dSmrg  * representations about the suitability of this software for any
361debfc3dSmrg  * purpose.  It is provided "as is" without express or implied warranty.
371debfc3dSmrg  *
381debfc3dSmrg  *
391debfc3dSmrg  * Copyright (c) 1996,1997
401debfc3dSmrg  * Silicon Graphics Computer Systems, Inc.
411debfc3dSmrg  *
421debfc3dSmrg  * Permission to use, copy, modify, distribute and sell this software
431debfc3dSmrg  * and its documentation for any purpose is hereby granted without fee,
441debfc3dSmrg  * provided that the above copyright notice appear in all copies and
451debfc3dSmrg  * that both that copyright notice and this permission notice appear
461debfc3dSmrg  * in supporting documentation.  Silicon Graphics makes no
471debfc3dSmrg  * representations about the suitability of this software for any
481debfc3dSmrg  * purpose.  It is provided "as is" without express or implied warranty.
491debfc3dSmrg  */
501debfc3dSmrg 
511debfc3dSmrg /** @file bits/stl_construct.h
521debfc3dSmrg  *  This is an internal header file, included by other library headers.
531debfc3dSmrg  *  Do not attempt to use it directly. @headername{memory}
541debfc3dSmrg  */
551debfc3dSmrg 
561debfc3dSmrg #ifndef _STL_CONSTRUCT_H
571debfc3dSmrg #define _STL_CONSTRUCT_H 1
581debfc3dSmrg 
591debfc3dSmrg #include <new>
601debfc3dSmrg #include <bits/move.h>
61*8feb0f0bSmrg #include <bits/stl_iterator_base_types.h> // for iterator_traits
62*8feb0f0bSmrg #include <bits/stl_iterator_base_funcs.h> // for advance
63*8feb0f0bSmrg 
64*8feb0f0bSmrg /* This file provides the C++17 functions std::destroy_at, std::destroy, and
65*8feb0f0bSmrg  * std::destroy_n, and the C++20 function std::construct_at.
66*8feb0f0bSmrg  * It also provides std::_Construct, std::_Destroy,and std::_Destroy_n functions
67*8feb0f0bSmrg  * which are defined in all standard modes and so can be used in C++98-14 code.
68*8feb0f0bSmrg  * The _Destroy functions will dispatch to destroy_at during constant
69*8feb0f0bSmrg  * evaluation, because calls to that function are intercepted by the compiler
70*8feb0f0bSmrg  * to allow use in constant expressions.
71*8feb0f0bSmrg  */
721debfc3dSmrg 
_GLIBCXX_VISIBILITY(default)731debfc3dSmrg namespace std _GLIBCXX_VISIBILITY(default)
741debfc3dSmrg {
751debfc3dSmrg _GLIBCXX_BEGIN_NAMESPACE_VERSION
761debfc3dSmrg 
77*8feb0f0bSmrg #if __cplusplus >= 201703L
78*8feb0f0bSmrg   template <typename _Tp>
79*8feb0f0bSmrg     _GLIBCXX20_CONSTEXPR inline void
80*8feb0f0bSmrg     destroy_at(_Tp* __location)
81*8feb0f0bSmrg     {
82*8feb0f0bSmrg       if constexpr (__cplusplus > 201703L && is_array_v<_Tp>)
83*8feb0f0bSmrg 	{
84*8feb0f0bSmrg 	  for (auto& __x : *__location)
85*8feb0f0bSmrg 	    std::destroy_at(std::__addressof(__x));
86*8feb0f0bSmrg 	}
87*8feb0f0bSmrg       else
88*8feb0f0bSmrg 	__location->~_Tp();
89*8feb0f0bSmrg     }
90*8feb0f0bSmrg 
91*8feb0f0bSmrg #if __cplusplus > 201703L
92*8feb0f0bSmrg   template<typename _Tp, typename... _Args>
93*8feb0f0bSmrg     constexpr auto
94*8feb0f0bSmrg     construct_at(_Tp* __location, _Args&&... __args)
95*8feb0f0bSmrg     noexcept(noexcept(::new((void*)0) _Tp(std::declval<_Args>()...)))
96*8feb0f0bSmrg     -> decltype(::new((void*)0) _Tp(std::declval<_Args>()...))
97*8feb0f0bSmrg     { return ::new((void*)__location) _Tp(std::forward<_Args>(__args)...); }
98*8feb0f0bSmrg #endif // C++20
99*8feb0f0bSmrg #endif// C++17
100*8feb0f0bSmrg 
1011debfc3dSmrg   /**
1021debfc3dSmrg    * Constructs an object in existing memory by invoking an allocated
1031debfc3dSmrg    * object's constructor with an initializer.
1041debfc3dSmrg    */
1051debfc3dSmrg #if __cplusplus >= 201103L
106*8feb0f0bSmrg   template<typename _Tp, typename... _Args>
1071debfc3dSmrg     inline void
108*8feb0f0bSmrg     _Construct(_Tp* __p, _Args&&... __args)
109*8feb0f0bSmrg     { ::new(static_cast<void*>(__p)) _Tp(std::forward<_Args>(__args)...); }
1101debfc3dSmrg #else
1111debfc3dSmrg   template<typename _T1, typename _T2>
1121debfc3dSmrg     inline void
1131debfc3dSmrg     _Construct(_T1* __p, const _T2& __value)
1141debfc3dSmrg     {
1151debfc3dSmrg       // _GLIBCXX_RESOLVE_LIB_DEFECTS
1161debfc3dSmrg       // 402. wrong new expression in [some_]allocator::construct
1171debfc3dSmrg       ::new(static_cast<void*>(__p)) _T1(__value);
1181debfc3dSmrg     }
1191debfc3dSmrg #endif
1201debfc3dSmrg 
1211debfc3dSmrg   template<typename _T1>
1221debfc3dSmrg     inline void
1231debfc3dSmrg     _Construct_novalue(_T1* __p)
1241debfc3dSmrg     { ::new(static_cast<void*>(__p)) _T1; }
1251debfc3dSmrg 
126*8feb0f0bSmrg   template<typename _ForwardIterator>
127*8feb0f0bSmrg     _GLIBCXX20_CONSTEXPR void
128*8feb0f0bSmrg     _Destroy(_ForwardIterator __first, _ForwardIterator __last);
129*8feb0f0bSmrg 
1301debfc3dSmrg   /**
1311debfc3dSmrg    * Destroy the object pointed to by a pointer type.
1321debfc3dSmrg    */
1331debfc3dSmrg   template<typename _Tp>
134*8feb0f0bSmrg     _GLIBCXX14_CONSTEXPR inline void
1351debfc3dSmrg     _Destroy(_Tp* __pointer)
136*8feb0f0bSmrg     {
137*8feb0f0bSmrg #if __cplusplus > 201703L
138*8feb0f0bSmrg       std::destroy_at(__pointer);
139*8feb0f0bSmrg #else
140*8feb0f0bSmrg       __pointer->~_Tp();
141*8feb0f0bSmrg #endif
142*8feb0f0bSmrg     }
1431debfc3dSmrg 
1441debfc3dSmrg   template<bool>
1451debfc3dSmrg     struct _Destroy_aux
1461debfc3dSmrg     {
1471debfc3dSmrg       template<typename _ForwardIterator>
148*8feb0f0bSmrg 	static _GLIBCXX20_CONSTEXPR void
1491debfc3dSmrg 	__destroy(_ForwardIterator __first, _ForwardIterator __last)
1501debfc3dSmrg 	{
1511debfc3dSmrg 	  for (; __first != __last; ++__first)
1521debfc3dSmrg 	    std::_Destroy(std::__addressof(*__first));
1531debfc3dSmrg 	}
1541debfc3dSmrg     };
1551debfc3dSmrg 
1561debfc3dSmrg   template<>
1571debfc3dSmrg     struct _Destroy_aux<true>
1581debfc3dSmrg     {
1591debfc3dSmrg       template<typename _ForwardIterator>
1601debfc3dSmrg         static void
1611debfc3dSmrg         __destroy(_ForwardIterator, _ForwardIterator) { }
1621debfc3dSmrg     };
1631debfc3dSmrg 
1641debfc3dSmrg   /**
1651debfc3dSmrg    * Destroy a range of objects.  If the value_type of the object has
1661debfc3dSmrg    * a trivial destructor, the compiler should optimize all of this
1671debfc3dSmrg    * away, otherwise the objects' destructors must be invoked.
1681debfc3dSmrg    */
1691debfc3dSmrg   template<typename _ForwardIterator>
170*8feb0f0bSmrg     _GLIBCXX20_CONSTEXPR inline void
1711debfc3dSmrg     _Destroy(_ForwardIterator __first, _ForwardIterator __last)
1721debfc3dSmrg     {
1731debfc3dSmrg       typedef typename iterator_traits<_ForwardIterator>::value_type
1741debfc3dSmrg                        _Value_type;
1751debfc3dSmrg #if __cplusplus >= 201103L
1761debfc3dSmrg       // A deleted destructor is trivial, this ensures we reject such types:
1771debfc3dSmrg       static_assert(is_destructible<_Value_type>::value,
1781debfc3dSmrg 		    "value type is destructible");
1791debfc3dSmrg #endif
180*8feb0f0bSmrg #if __cplusplus > 201703L && defined __cpp_lib_is_constant_evaluated
181*8feb0f0bSmrg       if (std::is_constant_evaluated())
182*8feb0f0bSmrg 	return _Destroy_aux<false>::__destroy(__first, __last);
183*8feb0f0bSmrg #endif
1841debfc3dSmrg       std::_Destroy_aux<__has_trivial_destructor(_Value_type)>::
1851debfc3dSmrg 	__destroy(__first, __last);
1861debfc3dSmrg     }
1871debfc3dSmrg 
1881debfc3dSmrg   template<bool>
1891debfc3dSmrg     struct _Destroy_n_aux
1901debfc3dSmrg     {
1911debfc3dSmrg       template<typename _ForwardIterator, typename _Size>
192*8feb0f0bSmrg 	static _GLIBCXX20_CONSTEXPR _ForwardIterator
1931debfc3dSmrg 	__destroy_n(_ForwardIterator __first, _Size __count)
1941debfc3dSmrg 	{
1951debfc3dSmrg 	  for (; __count > 0; (void)++__first, --__count)
1961debfc3dSmrg 	    std::_Destroy(std::__addressof(*__first));
1971debfc3dSmrg 	  return __first;
1981debfc3dSmrg 	}
1991debfc3dSmrg     };
2001debfc3dSmrg 
2011debfc3dSmrg   template<>
2021debfc3dSmrg     struct _Destroy_n_aux<true>
2031debfc3dSmrg     {
2041debfc3dSmrg       template<typename _ForwardIterator, typename _Size>
2051debfc3dSmrg         static _ForwardIterator
2061debfc3dSmrg         __destroy_n(_ForwardIterator __first, _Size __count)
2071debfc3dSmrg 	{
2081debfc3dSmrg 	  std::advance(__first, __count);
2091debfc3dSmrg 	  return __first;
2101debfc3dSmrg 	}
2111debfc3dSmrg     };
2121debfc3dSmrg 
2131debfc3dSmrg   /**
2141debfc3dSmrg    * Destroy a range of objects.  If the value_type of the object has
2151debfc3dSmrg    * a trivial destructor, the compiler should optimize all of this
2161debfc3dSmrg    * away, otherwise the objects' destructors must be invoked.
2171debfc3dSmrg    */
2181debfc3dSmrg   template<typename _ForwardIterator, typename _Size>
219*8feb0f0bSmrg     _GLIBCXX20_CONSTEXPR inline _ForwardIterator
2201debfc3dSmrg     _Destroy_n(_ForwardIterator __first, _Size __count)
2211debfc3dSmrg     {
2221debfc3dSmrg       typedef typename iterator_traits<_ForwardIterator>::value_type
2231debfc3dSmrg                        _Value_type;
2241debfc3dSmrg #if __cplusplus >= 201103L
2251debfc3dSmrg       // A deleted destructor is trivial, this ensures we reject such types:
2261debfc3dSmrg       static_assert(is_destructible<_Value_type>::value,
2271debfc3dSmrg 		    "value type is destructible");
2281debfc3dSmrg #endif
229*8feb0f0bSmrg #if __cplusplus > 201703L && defined __cpp_lib_is_constant_evaluated
230*8feb0f0bSmrg       if (std::is_constant_evaluated())
231*8feb0f0bSmrg 	return _Destroy_n_aux<false>::__destroy_n(__first, __count);
232*8feb0f0bSmrg #endif
2331debfc3dSmrg       return std::_Destroy_n_aux<__has_trivial_destructor(_Value_type)>::
2341debfc3dSmrg 	__destroy_n(__first, __count);
2351debfc3dSmrg     }
2361debfc3dSmrg 
237*8feb0f0bSmrg #if __cplusplus >= 201703L
2381debfc3dSmrg   template <typename _ForwardIterator>
239*8feb0f0bSmrg     _GLIBCXX20_CONSTEXPR inline void
2401debfc3dSmrg     destroy(_ForwardIterator __first, _ForwardIterator __last)
2411debfc3dSmrg     {
2421debfc3dSmrg       std::_Destroy(__first, __last);
2431debfc3dSmrg     }
2441debfc3dSmrg 
2451debfc3dSmrg   template <typename _ForwardIterator, typename _Size>
246*8feb0f0bSmrg     _GLIBCXX20_CONSTEXPR inline _ForwardIterator
2471debfc3dSmrg     destroy_n(_ForwardIterator __first, _Size __count)
2481debfc3dSmrg     {
2491debfc3dSmrg       return std::_Destroy_n(__first, __count);
2501debfc3dSmrg     }
251*8feb0f0bSmrg #endif // C++17
2521debfc3dSmrg 
2531debfc3dSmrg _GLIBCXX_END_NAMESPACE_VERSION
2541debfc3dSmrg } // namespace std
2551debfc3dSmrg 
2561debfc3dSmrg #endif /* _STL_CONSTRUCT_H */
257