{
typedef typename iterator_traits<_ForwardIterator>::value_type
_ValueType;
+
+ // Trivial types do not need a constructor to begin their lifetime,
+ // so try to use std::fill_n to benefit from its memmove optimization.
+ // For arbitrary class types and floating point types we can't assume
+ // that __n > 0 and std::__size_to_integer(__n) > 0 are equivalent,
+ // so only use std::fill_n when _Size is already an integral type.
#if __cplusplus < 201103L
- const bool __assignable = true;
+ const bool __can_fill = __is_integer<_Size>::__value;
#else
- // Trivial types can have deleted copy constructor, but the std::fill
+ // Trivial types can have deleted copy constructor, but the std::fill_n
// optimization that uses memmove would happily "copy" them anyway.
static_assert(is_constructible<_ValueType, const _Tp&>::value,
"result type must be constructible from input type");
- // Trivial types can have deleted assignment, so using std::fill
- // would be ill-formed. Require assignability before using std::fill:
- const bool __assignable = is_copy_assignable<_ValueType>::value;
+ // Trivial types can have deleted assignment, so using std::fill_n
+ // would be ill-formed. Require assignability before using std::fill_n:
+ constexpr bool __can_fill
+ = __and_<is_integral<_Size>, is_copy_assignable<_ValueType>>::value;
#endif
- return __uninitialized_fill_n<__is_trivial(_ValueType) && __assignable>::
+ return __uninitialized_fill_n<__is_trivial(_ValueType) && __can_fill>::
__uninit_fill_n(__first, __n, __x);
}
{
typedef typename iterator_traits<_ForwardIterator>::value_type
_ValueType;
- // trivial types can have deleted assignment
- const bool __assignable = is_copy_assignable<_ValueType>::value;
+ // See uninitialized_fill_n for the conditions for using std::fill_n.
+ constexpr bool __can_fill
+ = __and_<is_integral<_Size>, is_copy_assignable<_ValueType>>::value;
return __uninitialized_default_n_1<__is_trivial(_ValueType)
- && __assignable>::
+ && __can_fill>::
__uninit_default_n(__first, __n);
}
--- /dev/null
+// Copyright (C) 2020 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library. This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3. If not see
+// <http://www.gnu.org/licenses/>.
+
+// { dg-do run { target c++11 } }
+
+#include <memory>
+#include <algorithm>
+#include <testsuite_hooks.h>
+
+struct Value
+{
+ int value = 0x1234;
+};
+
+void
+test01()
+{
+ alignas(Value) unsigned char buf[3 * sizeof(Value) + 1];
+ std::fill(std::begin(buf), std::end(buf), 0xff);
+ const auto p = reinterpret_cast<Value*>(buf);
+ std::__uninitialized_default_n(p, 2.0001);
+ VERIFY( p[0].value == 0x1234 );
+ VERIFY( p[1].value == 0x1234 );
+ VERIFY( p[2].value == 0x1234 );
+ VERIFY( *std::prev(std::end(buf)) == 0xff );
+}
+
+void
+test02()
+{
+ // The standard only requires that n>0 and --n are valid expressions.
+ struct Size
+ {
+ int value;
+
+ void operator--() { --value; }
+
+ int operator>(void*) { return value != 0; }
+ };
+
+ alignas(Value) unsigned char buf[4 * sizeof(Value) + 1];
+ std::fill(std::begin(buf), std::end(buf), 0xff);
+ const auto p = reinterpret_cast<Value*>(buf);
+ Size n = {4};
+ std::__uninitialized_default_n(p, n);
+ VERIFY( p[0].value == 0x1234 );
+ VERIFY( p[1].value == 0x1234 );
+ VERIFY( p[2].value == 0x1234 );
+ VERIFY( p[3].value == 0x1234 );
+ VERIFY( *std::prev(std::end(buf)) == 0xff );
+}
+
+int
+main()
+{
+ test01();
+ test02();
+}
--- /dev/null
+// Copyright (C) 2020 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library. This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3. If not see
+// <http://www.gnu.org/licenses/>.
+
+// { dg-do run }
+
+#include <memory>
+#include <testsuite_hooks.h>
+
+void
+test01()
+{
+ int i[4] = { };
+ std::uninitialized_fill_n(i, 2.0001, 0xabcd);
+ VERIFY( i[0] == 0xabcd );
+ VERIFY( i[1] == 0xabcd );
+ VERIFY( i[2] == 0xabcd );
+ VERIFY( i[3] == 0 );
+}
+
+void
+test02()
+{
+ // The standard only requires that n>0 and --n are valid expressions.
+ struct Size
+ {
+ int value;
+
+ void operator--() { --value; }
+
+ int operator>(void*) { return value != 0; }
+ };
+
+ int i[5] = { };
+ Size n = {4};
+ std::uninitialized_fill_n(i, n, 0xdcba);
+ VERIFY( i[0] == 0xdcba );
+ VERIFY( i[1] == 0xdcba );
+ VERIFY( i[2] == 0xdcba );
+ VERIFY( i[3] == 0xdcba );
+ VERIFY( i[4] == 0 );
+}
+
+int
+main()
+{
+ test01();
+ test02();
+}
--- /dev/null
+// Copyright (C) 2020 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library. This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3. If not see
+// <http://www.gnu.org/licenses/>.
+
+// { dg-options "-std=gnu++17" }
+// { dg-do run { target c++17 } }
+
+#include <memory>
+#include <algorithm>
+#include <testsuite_hooks.h>
+
+struct Value
+{
+ int value = 0x1234;
+};
+
+void
+test01()
+{
+ alignas(Value) unsigned char buf[3 * sizeof(Value) + 1];
+ std::fill(std::begin(buf), std::end(buf), 0xff);
+ const auto p = reinterpret_cast<Value*>(buf);
+ std::uninitialized_value_construct_n(p, 2.0001);
+ VERIFY( p[0].value == 0x1234 );
+ VERIFY( p[1].value == 0x1234 );
+ VERIFY( p[2].value == 0x1234 );
+ VERIFY( *std::prev(std::end(buf)) == 0xff );
+}
+
+void
+test02()
+{
+ // The standard only requires that n>0 and --n are valid expressions.
+ struct Size
+ {
+ int value;
+
+ void operator--() { --value; }
+
+ int operator>(void*) { return value != 0; }
+ };
+
+ alignas(Value) unsigned char buf[4 * sizeof(Value) + 1];
+ std::fill(std::begin(buf), std::end(buf), 0xff);
+ const auto p = reinterpret_cast<Value*>(buf);
+ Size n = {4};
+ std::uninitialized_value_construct_n(p, n);
+ VERIFY( p[0].value == 0x1234 );
+ VERIFY( p[1].value == 0x1234 );
+ VERIFY( p[2].value == 0x1234 );
+ VERIFY( p[3].value == 0x1234 );
+ VERIFY( *std::prev(std::end(buf)) == 0xff );
+}
+
+int
+main()
+{
+ test01();
+ test02();
+}