libstdc++
stl_construct.h
Go to the documentation of this file.
1 // nonstandard construct and destroy functions -*- C++ -*-
2 
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
4 // 2009, 2010
5 // Free Software Foundation, Inc.
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
10 // Free Software Foundation; either version 3, or (at your option)
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 
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.
21 
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/>.
26 
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 
53 /** @file bits/stl_construct.h
54  * This is an internal header file, included by other library headers.
55  * Do not attempt to use it directly. @headername{memory}
56  */
57 
58 #ifndef _STL_CONSTRUCT_H
59 #define _STL_CONSTRUCT_H 1
60 
61 #include <new>
62 #include <bits/move.h>
63 
64 namespace std _GLIBCXX_VISIBILITY(default)
65 {
66 _GLIBCXX_BEGIN_NAMESPACE_VERSION
67 
68  /**
69  * Constructs an object in existing memory by invoking an allocated
70  * object's constructor with an initializer.
71  */
72 #ifdef __GXX_EXPERIMENTAL_CXX0X__
73  template<typename _T1, typename... _Args>
74  inline void
75  _Construct(_T1* __p, _Args&&... __args)
76  { ::new(static_cast<void*>(__p)) _T1(std::forward<_Args>(__args)...); }
77 #else
78  template<typename _T1, typename _T2>
79  inline void
80  _Construct(_T1* __p, const _T2& __value)
81  {
82  // _GLIBCXX_RESOLVE_LIB_DEFECTS
83  // 402. wrong new expression in [some_]allocator::construct
84  ::new(static_cast<void*>(__p)) _T1(__value);
85  }
86 #endif
87 
88  /**
89  * Destroy the object pointed to by a pointer type.
90  */
91  template<typename _Tp>
92  inline void
93  _Destroy(_Tp* __pointer)
94  { __pointer->~_Tp(); }
95 
96  template<bool>
97  struct _Destroy_aux
98  {
99  template<typename _ForwardIterator>
100  static void
101  __destroy(_ForwardIterator __first, _ForwardIterator __last)
102  {
103  for (; __first != __last; ++__first)
104  std::_Destroy(std::__addressof(*__first));
105  }
106  };
107 
108  template<>
109  struct _Destroy_aux<true>
110  {
111  template<typename _ForwardIterator>
112  static void
113  __destroy(_ForwardIterator, _ForwardIterator) { }
114  };
115 
116  /**
117  * Destroy a range of objects. If the value_type of the object has
118  * a trivial destructor, the compiler should optimize all of this
119  * away, otherwise the objects' destructors must be invoked.
120  */
121  template<typename _ForwardIterator>
122  inline void
123  _Destroy(_ForwardIterator __first, _ForwardIterator __last)
124  {
125  typedef typename iterator_traits<_ForwardIterator>::value_type
126  _Value_type;
127  std::_Destroy_aux<__has_trivial_destructor(_Value_type)>::
128  __destroy(__first, __last);
129  }
130 
131  /**
132  * Destroy a range of objects using the supplied allocator. For
133  * nondefault allocators we do not optimize away invocation of
134  * destroy() even if _Tp has a trivial destructor.
135  */
136 
137  template <typename _Tp> class allocator;
138 
139  template<typename _ForwardIterator, typename _Allocator>
140  void
141  _Destroy(_ForwardIterator __first, _ForwardIterator __last,
142  _Allocator& __alloc)
143  {
144  for (; __first != __last; ++__first)
145  __alloc.destroy(std::__addressof(*__first));
146  }
147 
148  template<typename _ForwardIterator, typename _Tp>
149  inline void
150  _Destroy(_ForwardIterator __first, _ForwardIterator __last,
151  allocator<_Tp>&)
152  {
153  _Destroy(__first, __last);
154  }
155 
156 _GLIBCXX_END_NAMESPACE_VERSION
157 } // namespace
158 
159 #endif /* _STL_CONSTRUCT_H */
160 
void _Destroy(_Tp *__pointer)
Definition: stl_construct.h:93
void _Construct(_T1 *__p, _Args &&...__args)
Definition: stl_construct.h:75