#include <utility>
#endif
+#if __cplusplus >= 201103L
+#include <type_traits>
+#endif
+
namespace std _GLIBCXX_VISIBILITY(default)
{
_GLIBCXX_BEGIN_NAMESPACE_VERSION
allocator<_Tp>&)
{ return std::__uninitialized_default_n(__first, __n); }
+ template<bool _TrivialValueType>
+ struct __uninitialized_default_novalue_1
+ {
+ template<typename _ForwardIterator>
+ static void
+ __uninit_default_novalue(_ForwardIterator __first,
+ _ForwardIterator __last)
+ {
+ _ForwardIterator __cur = __first;
+ __try
+ {
+ for (; __cur != __last; ++__cur)
+ std::_Construct_novalue(std::__addressof(*__cur));
+ }
+ __catch(...)
+ {
+ std::_Destroy(__first, __cur);
+ __throw_exception_again;
+ }
+ }
+ };
+
+ template<>
+ struct __uninitialized_default_novalue_1<true>
+ {
+ template<typename _ForwardIterator>
+ static void
+ __uninit_default_novalue(_ForwardIterator __first,
+ _ForwardIterator __last)
+ {
+ }
+ };
+
+ template<bool _TrivialValueType>
+ struct __uninitialized_default_novalue_n_1
+ {
+ template<typename _ForwardIterator, typename _Size>
+ static _ForwardIterator
+ __uninit_default_novalue_n(_ForwardIterator __first, _Size __n)
+ {
+ _ForwardIterator __cur = __first;
+ __try
+ {
+ for (; __n > 0; --__n, ++__cur)
+ std::_Construct_novalue(std::__addressof(*__cur));
+ return __cur;
+ }
+ __catch(...)
+ {
+ std::_Destroy(__first, __cur);
+ __throw_exception_again;
+ }
+ }
+ };
+
+ template<>
+ struct __uninitialized_default_novalue_n_1<true>
+ {
+ template<typename _ForwardIterator, typename _Size>
+ static _ForwardIterator
+ __uninit_default_novalue_n(_ForwardIterator __first, _Size __n)
+ {
+ }
+ };
+
+ // __uninitialized_default_novalue
+ // Fills [first, last) with std::distance(first, last) default-initialized
+ // value_types(s).
+ template<typename _ForwardIterator>
+ inline void
+ __uninitialized_default_novalue(_ForwardIterator __first,
+ _ForwardIterator __last)
+ {
+ typedef typename iterator_traits<_ForwardIterator>::value_type
+ _ValueType;
+
+ std::__uninitialized_default_novalue_1<
+ is_trivially_default_constructible<_ValueType>::value>::
+ __uninit_default_novalue(__first, __last);
+ }
+
+ // __uninitialized_default_n
+ // Fills [first, first + n) with n default-initialized value_type(s).
+ template<typename _ForwardIterator, typename _Size>
+ inline _ForwardIterator
+ __uninitialized_default_novalue_n(_ForwardIterator __first, _Size __n)
+ {
+ typedef typename iterator_traits<_ForwardIterator>::value_type
+ _ValueType;
+
+ return __uninitialized_default_novalue_n_1<
+ is_trivially_default_constructible<_ValueType>::value>::
+ __uninit_default_novalue_n(__first, __n);
+ }
template<typename _InputIterator, typename _Size,
typename _ForwardIterator>
random_access_iterator_tag)
{ return std::uninitialized_copy(__first, __first + __n, __result); }
+ template<typename _InputIterator, typename _Size,
+ typename _ForwardIterator>
+ pair<_InputIterator, _ForwardIterator>
+ __uninitialized_copy_n_pair(_InputIterator __first, _Size __n,
+ _ForwardIterator __result, input_iterator_tag)
+ {
+ _ForwardIterator __cur = __result;
+ __try
+ {
+ for (; __n > 0; --__n, ++__first, ++__cur)
+ std::_Construct(std::__addressof(*__cur), *__first);
+ return {__first, __cur};
+ }
+ __catch(...)
+ {
+ std::_Destroy(__result, __cur);
+ __throw_exception_again;
+ }
+ }
+
+ template<typename _RandomAccessIterator, typename _Size,
+ typename _ForwardIterator>
+ inline pair<_RandomAccessIterator, _ForwardIterator>
+ __uninitialized_copy_n_pair(_RandomAccessIterator __first, _Size __n,
+ _ForwardIterator __result,
+ random_access_iterator_tag)
+ {
+ auto __second_res = uninitialized_copy(__first, __first + __n, __result);
+ auto __first_res = std::next(__first, __n);
+ return {__first_res, __second_res};
+ }
+
/**
* @brief Copies the range [first,first+n) into result.
* @param __first An input iterator.
_ForwardIterator __result)
{ return std::__uninitialized_copy_n(__first, __n, __result,
std::__iterator_category(__first)); }
+
+ template<typename _InputIterator, typename _Size, typename _ForwardIterator>
+ inline pair<_InputIterator, _ForwardIterator>
+ __uninitialized_copy_n_pair(_InputIterator __first, _Size __n,
+ _ForwardIterator __result)
+ {
+ return
+ std::__uninitialized_copy_n_pair(__first, __n, __result,
+ std::__iterator_category(__first));
+ }
+
#endif
#if __cplusplus > 201402L
uninitialized_default_construct(_ForwardIterator __first,
_ForwardIterator __last)
{
- for (; __first != __last; ++__first)
- ::new (static_cast<void*>(std::__addressof(*__first)))
- typename iterator_traits<_ForwardIterator>::value_type;
+ __uninitialized_default_novalue(__first, __last);
}
template <typename _ForwardIterator, typename _Size>
inline _ForwardIterator
uninitialized_default_construct_n(_ForwardIterator __first, _Size __count)
{
- for (; __count > 0; (void)++__first, --__count)
- ::new (static_cast<void*>(std::__addressof(*__first)))
- typename iterator_traits<_ForwardIterator>::value_type;
- return __first;
+ return __uninitialized_default_novalue_n(__first, __count);
}
template <typename _ForwardIterator>
uninitialized_value_construct(_ForwardIterator __first,
_ForwardIterator __last)
{
- for (; __first != __last; ++__first)
- ::new (static_cast<void*>(std::__addressof(*__first)))
- typename iterator_traits<_ForwardIterator>::value_type();
+ return __uninitialized_default(__first, __last);
}
template <typename _ForwardIterator, typename _Size>
inline _ForwardIterator
uninitialized_value_construct_n(_ForwardIterator __first, _Size __count)
{
- for (; __count > 0; (void)++__first, --__count)
- ::new (static_cast<void*>(std::__addressof(*__first)))
- typename iterator_traits<_ForwardIterator>::value_type();
- return __first;
+ return __uninitialized_default_n(__first, __count);
}
template <typename _InputIterator, typename _ForwardIterator>
uninitialized_move(_InputIterator __first, _InputIterator __last,
_ForwardIterator __result)
{
- for (; __first != __last; (void)++__result, ++__first)
- ::new (static_cast<void*>(std::__addressof(*__result)))
- typename
- iterator_traits<_ForwardIterator>::value_type(std::move(*__first));
- return __result;
+ return std::uninitialized_copy
+ (_GLIBCXX_MAKE_MOVE_ITERATOR(__first),
+ _GLIBCXX_MAKE_MOVE_ITERATOR(__last), __result);
}
template <typename _InputIterator, typename _Size, typename _ForwardIterator>
uninitialized_move_n(_InputIterator __first, _Size __count,
_ForwardIterator __result)
{
- for (; __count > 0; ++__result, (void) ++__first, --__count)
- ::new (static_cast<void*>(std::__addressof(*__result)))
- typename
- iterator_traits<_ForwardIterator>::value_type(std::move(*__first));
- return {__first, __result};
+ auto __res = std::__uninitialized_copy_n_pair
+ (_GLIBCXX_MAKE_MOVE_ITERATOR(__first),
+ __count, __result);
+ return {__res.first.base(), __res.second};
}
template <typename _Tp>
inline void
destroy_at(_Tp* __location)
{
- __location->~_Tp();
+ std::_Destroy(__location);
}
template <typename _ForwardIterator>
inline void
destroy(_ForwardIterator __first, _ForwardIterator __last)
{
- for (; __first != __last; ++__first)
- std::destroy_at(std::__addressof(*__first));
+ std::_Destroy(__first, __last);
}
template <typename _ForwardIterator, typename _Size>
inline _ForwardIterator
destroy_n(_ForwardIterator __first, _Size __count)
{
- for (; __count > 0; (void)++__first, --__count)
- std::destroy_at(std::__addressof(*__first));
- return __first;
+ return std::_Destroy_n(__first, __count);
}
#endif
#include <testsuite_hooks.h>
#include <string>
#include <array>
+#include <sstream>
int del_count = 0;
+int ctor_count = 0;
+int throw_after = 0;
struct DelCount
{
~DelCount() { ++del_count; }
};
+struct ThrowAfterN
+{
+ ThrowAfterN()
+ {
+ if (++ctor_count == throw_after) {
+ std::ostringstream os;
+ os << "ThrowAfterN(), ctor_count: " << ctor_count
+ << " throw_after: " << throw_after << std::endl;
+ throw std::runtime_error(os.str());
+ }
+ }
+ ThrowAfterN(ThrowAfterN&&)
+ {
+ if (++ctor_count == throw_after) {
+ std::ostringstream os;
+ os << "ThrowAfterN(), ctor_count: " << ctor_count
+ << " throw_after: " << throw_after << std::endl;
+ throw std::runtime_error(os.str());
+ }
+ }
+ DelCount dc;
+};
+
void test01()
{
char test_data[] = "123456";
free(target);
}
+void test10()
+{
+ char* x = (char*)malloc(sizeof(char)*10);
+ for (int i = 0; i < 10; ++i) new (x+i) char;
+ std::destroy(x, x+10);
+ free(x);
+}
+
+void test11()
+{
+ char* x = (char*)malloc(sizeof(char)*10);
+ for (int i = 0; i < 10; ++i) new (x+i) char;
+ std::destroy_n(x, 10);
+ free(x);
+}
+
+void test12()
+{
+ throw_after = 5;
+ del_count = 0;
+ ctor_count = 0;
+ ThrowAfterN* target =
+ (ThrowAfterN*)malloc(sizeof(ThrowAfterN)*10);
+ try {
+ std::uninitialized_default_construct(target, target+10);
+ } catch (...) {
+ }
+ VERIFY(ctor_count == 5);
+ VERIFY(del_count == 5);
+ throw_after = 0;
+ del_count = 0;
+ ctor_count = 0;
+}
+
+void test13()
+{
+ throw_after = 5;
+ del_count = 0;
+ ctor_count = 0;
+ ThrowAfterN* target =
+ (ThrowAfterN*)malloc(sizeof(ThrowAfterN)*10);
+ try {
+ std::uninitialized_value_construct(target, target+10);
+ } catch (...) {
+ }
+ VERIFY(ctor_count == 5);
+ VERIFY(del_count == 5);
+ throw_after = 0;
+ del_count = 0;
+ ctor_count = 0;
+}
+
+void test14()
+{
+ throw_after = 5;
+ del_count = 0;
+ ctor_count = 0;
+ ThrowAfterN* target =
+ (ThrowAfterN*)malloc(sizeof(ThrowAfterN)*10);
+ try {
+ std::uninitialized_default_construct_n(target, 10);
+ } catch (...) {
+ }
+ VERIFY(ctor_count == 5);
+ VERIFY(del_count == 5);
+ throw_after = 0;
+ del_count = 0;
+ ctor_count = 0;
+}
+
+void test15()
+{
+ throw_after = 5;
+ del_count = 0;
+ ctor_count = 0;
+ ThrowAfterN* target =
+ (ThrowAfterN*)malloc(sizeof(ThrowAfterN)*10);
+ try {
+ std::uninitialized_value_construct_n(target, 10);
+ } catch (...) {
+ }
+ VERIFY(ctor_count == 5);
+ VERIFY(del_count == 5);
+ throw_after = 0;
+ del_count = 0;
+ ctor_count = 0;
+}
+
+void test16()
+{
+ std::vector<ThrowAfterN> source(10);
+ del_count = 0;
+ ctor_count = 0;
+ throw_after = 5;
+ throw_after = 5;
+ ThrowAfterN* target =
+ (ThrowAfterN*)malloc(sizeof(ThrowAfterN)*10);
+ try {
+ std::uninitialized_move(source.begin(), source.end(), target);
+ } catch (...) {
+ }
+ VERIFY(ctor_count == 5);
+ VERIFY(del_count == 5);
+ throw_after = 0;
+ del_count = 0;
+ ctor_count = 0;
+}
+
+void test17()
+{
+ std::vector<ThrowAfterN> source(10);
+ del_count = 0;
+ ctor_count = 0;
+ throw_after = 5;
+ ThrowAfterN* target =
+ (ThrowAfterN*)malloc(sizeof(ThrowAfterN)*10);
+ try {
+ std::uninitialized_move_n(source.begin(), 10, target);
+ } catch (...) {
+ }
+ VERIFY(ctor_count == 5);
+ VERIFY(del_count == 5);
+ throw_after = 0;
+ del_count = 0;
+ ctor_count = 0;
+}
+
+void test18()
+{
+ char test_source[] = "123456";
+ char test_target[] = "000000";
+ std::uninitialized_move(std::begin(test_source),
+ std::end(test_source),
+ test_target);
+ VERIFY(std::string(test_target) == "123456");
+}
+
+void test19()
+{
+ char test_source[] = "123456";
+ char test_target[] = "000000";
+ std::uninitialized_move_n(std::begin(test_source),
+ 6,
+ test_target);
+ VERIFY(std::string(test_target) == "123456");
+}
+
int main()
{
test01();
test07();
test08();
test09();
+ test10();
+ test11();
+ test12();
+ test13();
+ test14();
+ test15();
+ test16();
+ test17();
+ test18();
+ test19();
}