]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/include/bits/stl_construct.h
* many: Replace uses of __GXX_EXPERIMENTAL_CXX0X__ with __cplusplus.
[thirdparty/gcc.git] / libstdc++-v3 / include / bits / stl_construct.h
CommitLineData
42526146
PE
1// nonstandard construct and destroy functions -*- C++ -*-
2
882b3d5c 3// Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
bd9db3b8 4// 2009, 2010, 2011
ff2ea587 5// Free Software Foundation, Inc.
42526146
PE
6//
7// This file is part of the GNU ISO C++ Library. This library is free
8// software; you can redistribute it and/or modify it under the
9// terms of the GNU General Public License as published by the
748086b7 10// Free Software Foundation; either version 3, or (at your option)
42526146
PE
11// any later version.
12
13// This library is distributed in the hope that it will be useful,
14// but WITHOUT ANY WARRANTY; without even the implied warranty of
15// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16// GNU General Public License for more details.
17
748086b7
JJ
18// Under Section 7 of GPL version 3, you are granted additional
19// permissions described in the GCC Runtime Library Exception, version
20// 3.1, as published by the Free Software Foundation.
42526146 21
748086b7
JJ
22// You should have received a copy of the GNU General Public License and
23// a copy of the GCC Runtime Library Exception along with this program;
24// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25// <http://www.gnu.org/licenses/>.
42526146 26
725dc051
BK
27/*
28 *
29 * Copyright (c) 1994
30 * Hewlett-Packard Company
31 *
32 * Permission to use, copy, modify, distribute and sell this software
33 * and its documentation for any purpose is hereby granted without fee,
34 * provided that the above copyright notice appear in all copies and
35 * that both that copyright notice and this permission notice appear
36 * in supporting documentation. Hewlett-Packard Company makes no
37 * representations about the suitability of this software for any
38 * purpose. It is provided "as is" without express or implied warranty.
39 *
40 *
41 * Copyright (c) 1996,1997
42 * Silicon Graphics Computer Systems, Inc.
43 *
44 * Permission to use, copy, modify, distribute and sell this software
45 * and its documentation for any purpose is hereby granted without fee,
46 * provided that the above copyright notice appear in all copies and
47 * that both that copyright notice and this permission notice appear
48 * in supporting documentation. Silicon Graphics makes no
49 * representations about the suitability of this software for any
50 * purpose. It is provided "as is" without express or implied warranty.
51 */
52
f910786b 53/** @file bits/stl_construct.h
729e3d3f 54 * This is an internal header file, included by other library headers.
f910786b 55 * Do not attempt to use it directly. @headername{memory}
725dc051
BK
56 */
57
3d7c150e
BK
58#ifndef _STL_CONSTRUCT_H
59#define _STL_CONSTRUCT_H 1
725dc051 60
a6863e25 61#include <new>
6401164d 62#include <bits/move.h>
e8eb60bd 63#include <ext/alloc_traits.h>
725dc051 64
12ffa228
BK
65namespace std _GLIBCXX_VISIBILITY(default)
66{
67_GLIBCXX_BEGIN_NAMESPACE_VERSION
3cbc7af0 68
494fff4c 69 /**
b0037845
PE
70 * Constructs an object in existing memory by invoking an allocated
71 * object's constructor with an initializer.
494fff4c 72 */
734f5023 73#if __cplusplus >= 201103L
fe27aa8b
PC
74 template<typename _T1, typename... _Args>
75 inline void
76 _Construct(_T1* __p, _Args&&... __args)
77 { ::new(static_cast<void*>(__p)) _T1(std::forward<_Args>(__args)...); }
55dd8445 78#else
fe27aa8b
PC
79 template<typename _T1, typename _T2>
80 inline void
494fff4c 81 _Construct(_T1* __p, const _T2& __value)
9dbaa948
PC
82 {
83 // _GLIBCXX_RESOLVE_LIB_DEFECTS
84 // 402. wrong new expression in [some_]allocator::construct
fe27aa8b 85 ::new(static_cast<void*>(__p)) _T1(__value);
9dbaa948 86 }
fe27aa8b 87#endif
ed6814f7 88
da73f9de 89 /**
da73f9de 90 * Destroy the object pointed to by a pointer type.
da73f9de 91 */
d78e147a 92 template<typename _Tp>
da73f9de
PC
93 inline void
94 _Destroy(_Tp* __pointer)
95 { __pointer->~_Tp(); }
96
cf0e6fff
PC
97 template<bool>
98 struct _Destroy_aux
99 {
100 template<typename _ForwardIterator>
101 static void
102 __destroy(_ForwardIterator __first, _ForwardIterator __last)
103 {
104 for (; __first != __last; ++__first)
882b3d5c 105 std::_Destroy(std::__addressof(*__first));
cf0e6fff
PC
106 }
107 };
108
109 template<>
110 struct _Destroy_aux<true>
111 {
112 template<typename _ForwardIterator>
113 static void
114 __destroy(_ForwardIterator, _ForwardIterator) { }
115 };
116
494fff4c
SW
117 /**
118 * Destroy a range of objects. If the value_type of the object has
119 * a trivial destructor, the compiler should optimize all of this
120 * away, otherwise the objects' destructors must be invoked.
494fff4c 121 */
d78e147a 122 template<typename _ForwardIterator>
494fff4c
SW
123 inline void
124 _Destroy(_ForwardIterator __first, _ForwardIterator __last)
125 {
126 typedef typename iterator_traits<_ForwardIterator>::value_type
127 _Value_type;
cf0e6fff
PC
128 std::_Destroy_aux<__has_trivial_destructor(_Value_type)>::
129 __destroy(__first, __last);
494fff4c 130 }
1985f1cd
MA
131
132 /**
1985f1cd
MA
133 * Destroy a range of objects using the supplied allocator. For
134 * nondefault allocators we do not optimize away invocation of
135 * destroy() even if _Tp has a trivial destructor.
1985f1cd
MA
136 */
137
138 template <typename _Tp> class allocator;
139
140 template<typename _ForwardIterator, typename _Allocator>
141 void
142 _Destroy(_ForwardIterator __first, _ForwardIterator __last,
c531371e 143 _Allocator& __alloc)
1985f1cd 144 {
e8eb60bd 145 typedef __gnu_cxx::__alloc_traits<_Allocator> __traits;
1985f1cd 146 for (; __first != __last; ++__first)
e8eb60bd 147 __traits::destroy(__alloc, std::__addressof(*__first));
1985f1cd
MA
148 }
149
9191d641 150 template<typename _ForwardIterator, typename _Tp>
1985f1cd
MA
151 inline void
152 _Destroy(_ForwardIterator __first, _ForwardIterator __last,
c531371e 153 allocator<_Tp>&)
1985f1cd
MA
154 {
155 _Destroy(__first, __last);
156 }
1985f1cd 157
12ffa228
BK
158_GLIBCXX_END_NAMESPACE_VERSION
159} // namespace
725dc051 160
3d7c150e 161#endif /* _STL_CONSTRUCT_H */
725dc051 162