libstdc++: stl_construct.h Source File (original) (raw)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56#ifndef _STL_CONSTRUCT_H

57#define _STL_CONSTRUCT_H 1

58

59#include <new>

63

64

65

66

67

68

69

70

71

72

73namespace std _GLIBCXX_VISIBILITY(default)

74{

75_GLIBCXX_BEGIN_NAMESPACE_VERSION

76

77#if __glibcxx_raw_memory_algorithms

78 template <typename _Tp>

79 _GLIBCXX20_CONSTEXPR inline void

80 destroy_at(_Tp* __location)

81 {

82 if constexpr (__cplusplus > 201703L && is_array_v<_Tp>)

83 {

84 for (auto& __x : *__location)

86 }

87 else

88 __location->~_Tp();

89 }

90

91#if __cpp_constexpr_dynamic_alloc

92 template<typename _Tp, typename... _Args>

93 constexpr auto

94 construct_at(_Tp* __location, _Args&&... __args)

95 noexcept(noexcept(::new((void*)0) _Tp(std::declval<_Args>()...)))

96 -> decltype(::new((void*)0) _Tp(std::declval<_Args>()...))

97 { return ::new((void*)__location) _Tp(std::forward<_Args>(__args)...); }

98#endif

99#endif

100

101

102

103

104

105#if __cplusplus >= 201103L

106 template<typename _Tp, typename... _Args>

107 _GLIBCXX20_CONSTEXPR

108 inline void

110 {

111#if __cpp_constexpr_dynamic_alloc

112 if (std::__is_constant_evaluated())

113 {

114

115 std::construct_at(__p, std::forward<_Args>(__args)...);

116 return;

117 }

118#endif

119 ::new((void*)__p) _Tp(std::forward<_Args>(__args)...);

120 }

121#else

122 template<typename _T1, typename _T2>

123 inline void

124 _Construct(_T1* __p, const _T2& __value)

125 {

126

127

128 ::new(static_cast<void*>(__p)) _T1(__value);

129 }

130#endif

131

132 template<typename _T1>

133 inline void

134 _Construct_novalue(_T1* __p)

135 { ::new((void*)__p) _T1; }

136

137 template<typename _ForwardIterator>

138 _GLIBCXX20_CONSTEXPR void

139 _Destroy(_ForwardIterator __first, _ForwardIterator __last);

140

141

142

143

144 template<typename _Tp>

145 _GLIBCXX14_CONSTEXPR inline void

147 {

148#if __cpp_constexpr_dynamic_alloc

149 std::destroy_at(__pointer);

150#else

151 __pointer->~_Tp();

152#endif

153 }

154

155 template<bool>

156 struct _Destroy_aux

157 {

158 template<typename _ForwardIterator>

159 static _GLIBCXX20_CONSTEXPR void

160 __destroy(_ForwardIterator __first, _ForwardIterator __last)

161 {

162 for (; __first != __last; ++__first)

164 }

165 };

166

167 template<>

168 struct _Destroy_aux

169 {

170 template<typename _ForwardIterator>

171 static void

172 __destroy(_ForwardIterator, _ForwardIterator) { }

173 };

174

175

176

177

178

179

180 template<typename _ForwardIterator>

181 _GLIBCXX20_CONSTEXPR inline void

182 _Destroy(_ForwardIterator __first, _ForwardIterator __last)

183 {

185 _Value_type;

186#if __cplusplus >= 201103L

187

189 "value type is destructible");

190#endif

191#if __cpp_constexpr_dynamic_alloc

192 if (std::__is_constant_evaluated())

193 return std::_Destroy_aux::__destroy(__first, __last);

194#endif

195 std::_Destroy_aux<__has_trivial_destructor(_Value_type)>::

196 __destroy(__first, __last);

197 }

198

199 template<bool>

200 struct _Destroy_n_aux

201 {

202 template<typename _ForwardIterator, typename _Size>

203 static _GLIBCXX20_CONSTEXPR _ForwardIterator

204 __destroy_n(_ForwardIterator __first, _Size __count)

205 {

206 for (; __count > 0; (void)++__first, --__count)

208 return __first;

209 }

210 };

211

212 template<>

213 struct _Destroy_n_aux

214 {

215 template<typename _ForwardIterator, typename _Size>

216 static _ForwardIterator

217 __destroy_n(_ForwardIterator __first, _Size __count)

218 {

220 return __first;

221 }

222 };

223

224

225

226

227

228

229 template<typename _ForwardIterator, typename _Size>

230 _GLIBCXX20_CONSTEXPR inline _ForwardIterator

231 _Destroy_n(_ForwardIterator __first, _Size __count)

232 {

234 _Value_type;

235#if __cplusplus >= 201103L

236

238 "value type is destructible");

239#endif

240#if __cpp_constexpr_dynamic_alloc

241 if (std::__is_constant_evaluated())

242 return std::_Destroy_n_aux::__destroy_n(__first, __count);

243#endif

244 return std::_Destroy_n_aux<__has_trivial_destructor(_Value_type)>::

245 __destroy_n(__first, __count);

246 }

247

248#if __glibcxx_raw_memory_algorithms

249 template <typename _ForwardIterator>

250 _GLIBCXX20_CONSTEXPR inline void

251 destroy(_ForwardIterator __first, _ForwardIterator __last)

252 {

254 }

255

256 template <typename _ForwardIterator, typename _Size>

257 _GLIBCXX20_CONSTEXPR inline _ForwardIterator

258 destroy_n(_ForwardIterator __first, _Size __count)

259 {

261 }

262#endif

263

264_GLIBCXX_END_NAMESPACE_VERSION

265}

266

267#endif

constexpr _Tp * __addressof(_Tp &__r) noexcept

Same as C++11 std::addressof.

ISO C++ entities toplevel namespace is std.

constexpr _ForwardIterator _Destroy_n(_ForwardIterator __first, _Size __count)

constexpr void _Construct(_Tp *__p, _Args &&... __args)

constexpr void advance(_InputIterator &__i, _Distance __n)

A generalization of pointer arithmetic.

constexpr void _Destroy(_ForwardIterator __first, _ForwardIterator __last)

Traits class for iterators.