Commit 613c932f by Patrick Palka

libstdc++: Implement ranges [specialized.algorithms]

This implements all the ranges members defined in [specialized.algorithms]:

  ranges::uninitialized_default_construct
  ranges::uninitialized_value_construct
  ranges::uninitialized_copy
  ranges::uninitialized_copy_n
  ranges::uninitialized_move
  ranges::uninitialized_move_n
  ranges::uninitialized_fill
  ranges::uninitialized_fill_n
  ranges::construct_at
  ranges::destroy_at
  ranges::destroy

It also implements (hopefully correctly) the "obvious" optimizations for these
algos, namely that if the output range has a trivial value type and if the
appropriate operation won't throw then we can dispatch to the standard ranges
version of the algorithm which will then potentially enable further
optimizations.

libstdc++-v3/ChangeLog:

	* include/Makefile.am: Add <bits/ranges_uninitialized.h>.
	* include/Makefile.in: Regenerate.
	* include/bits/ranges_uninitialized.h: New header.
	* include/std/memory: Include it.
	* testsuite/20_util/specialized_algorithms/destroy/constrained.cc: New
	test.
	* .../uninitialized_copy/constrained.cc: New test.
	* .../uninitialized_default_construct/constrained.cc: New test.
	* .../uninitialized_fill/constrained.cc: New test.
	* .../uninitialized_move/constrained.cc: New test.
	* .../uninitialized_value_construct/constrained.cc: New test.
parent 90fc7b3c
2020-02-13 Patrick Palka <ppalka@redhat.com> 2020-02-13 Patrick Palka <ppalka@redhat.com>
* include/Makefile.am: Add <bits/ranges_uninitialized.h>.
* include/Makefile.in: Regenerate.
* include/bits/ranges_uninitialized.h: New header.
* include/std/memory: Include it.
* testsuite/20_util/specialized_algorithms/destroy/constrained.cc: New
test.
* .../uninitialized_copy/constrained.cc: New test.
* .../uninitialized_default_construct/constrained.cc: New test.
* .../uninitialized_fill/constrained.cc: New test.
* .../uninitialized_move/constrained.cc: New test.
* .../uninitialized_value_construct/constrained.cc: New test.
* include/Makefile.am: Add bits/ranges_algobase.h * include/Makefile.am: Add bits/ranges_algobase.h
* include/Makefile.in: Regenerate. * include/Makefile.in: Regenerate.
* bits/ranges_algo.h: Include <bits/ranges_algobase.h> and refactor * bits/ranges_algo.h: Include <bits/ranges_algobase.h> and refactor
......
...@@ -159,6 +159,7 @@ bits_headers = \ ...@@ -159,6 +159,7 @@ bits_headers = \
${bits_srcdir}/range_cmp.h \ ${bits_srcdir}/range_cmp.h \
${bits_srcdir}/ranges_algobase.h \ ${bits_srcdir}/ranges_algobase.h \
${bits_srcdir}/ranges_algo.h \ ${bits_srcdir}/ranges_algo.h \
${bits_srcdir}/ranges_uninitialized.h \
${bits_srcdir}/refwrap.h \ ${bits_srcdir}/refwrap.h \
${bits_srcdir}/regex.h \ ${bits_srcdir}/regex.h \
${bits_srcdir}/regex.tcc \ ${bits_srcdir}/regex.tcc \
......
...@@ -504,6 +504,7 @@ bits_headers = \ ...@@ -504,6 +504,7 @@ bits_headers = \
${bits_srcdir}/range_cmp.h \ ${bits_srcdir}/range_cmp.h \
${bits_srcdir}/ranges_algobase.h \ ${bits_srcdir}/ranges_algobase.h \
${bits_srcdir}/ranges_algo.h \ ${bits_srcdir}/ranges_algo.h \
${bits_srcdir}/ranges_uninitialized.h \
${bits_srcdir}/refwrap.h \ ${bits_srcdir}/refwrap.h \
${bits_srcdir}/regex.h \ ${bits_srcdir}/regex.h \
${bits_srcdir}/regex.tcc \ ${bits_srcdir}/regex.tcc \
......
...@@ -66,6 +66,7 @@ ...@@ -66,6 +66,7 @@
#include <bits/stl_uninitialized.h> #include <bits/stl_uninitialized.h>
#include <bits/stl_tempbuf.h> #include <bits/stl_tempbuf.h>
#include <bits/stl_raw_storage_iter.h> #include <bits/stl_raw_storage_iter.h>
#include <bits/ranges_uninitialized.h>
#if __cplusplus >= 201103L #if __cplusplus >= 201103L
# include <exception> // std::exception # include <exception> // std::exception
......
// 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++2a" }
// { dg-do run { target c++2a } }
#include <algorithm>
#include <cstring>
#include <deque>
#include <list>
#include <memory>
#include <span>
#include <string>
#include <vector>
#include <testsuite_hooks.h>
#include <testsuite_iterators.h>
namespace ranges = std::ranges;
struct X
{
X()
{ ++count; }
~X()
{ --count; }
static inline int count = 0;
};
void
test01()
{
for (int k = 0; k < 3; k++)
{
constexpr int size = 1024;
auto buffer = std::unique_ptr<char[]>(new char[sizeof(X)*size]);
std::span<X> rx((X *)buffer.get(), size);
ranges::uninitialized_default_construct(rx);
VERIFY( X::count == size );
auto i = rx.cbegin();
if (k == 0)
i = ranges::destroy(rx);
else if (k == 1)
i = ranges::destroy(rx.begin(), rx.end());
else if (k == 2)
i = ranges::destroy_n(rx.begin(), size);
else
__builtin_abort();
VERIFY( i == rx.cend() );
VERIFY( X::count == 0 );
}
}
int
main()
{
test01();
}
// 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++2a" }
// { dg-do run { target c++2a } }
#include <algorithm>
#include <cstring>
#include <deque>
#include <list>
#include <memory>
#include <span>
#include <string>
#include <vector>
#include <testsuite_hooks.h>
#include <testsuite_iterators.h>
using __gnu_test::test_input_range;
using __gnu_test::test_forward_range;
namespace ranges = std::ranges;
template<typename T>
void
test01(const std::vector<T> &ix)
{
static_assert(std::copy_constructible<T>);
static_assert(std::equality_comparable<T>);
for (int k = 0; k < 7; k++)
{
int size = ix.size();
auto buffer = std::unique_ptr<char[]>(new char[sizeof(T)*size]);
std::span<T> rx((T *)buffer.get(), size);
ranges::uninitialized_copy_result res = {ix.cbegin(), rx.cbegin()};
if (k == 0)
res = ranges::uninitialized_copy(ix.begin(), ix.end(),
rx.begin(), rx.end());
else if (k == 1)
res = ranges::uninitialized_copy(ix, rx);
else if (k == 2)
res = ranges::uninitialized_copy_n(ix.begin(), size,
rx.begin(), rx.end());
else if (k == 3)
res = ranges::uninitialized_copy(ix.begin(), ix.end(),
rx.cbegin(), rx.cend());
else if (k == 4)
res = ranges::uninitialized_copy(ix, std::as_const(rx));
else if (k == 5)
res = ranges::uninitialized_copy_n(ix.begin(), size,
rx.cbegin(), rx.cend());
else if (k == 6)
res = ranges::uninitialized_copy_n(ix.begin(), size/2,
rx.cbegin(), rx.cend());
else if (k == 7)
res = ranges::uninitialized_copy_n(ix.begin(), size,
rx.cbegin(), rx.cbegin()+size/2);
else
__builtin_abort();
if (k == 6 || k == 7)
{
VERIFY( ranges::distance(ix.cbegin(), res.in) == size/2 );
VERIFY( ranges::distance(rx.cbegin(), res.out) == size/2 );
VERIFY( ranges::equal(ix.begin(), ix.begin()+size/2,
rx.begin(), rx.begin()+size/2) );
ranges::destroy(rx.begin(), rx.begin()+size/2);
}
else
{
VERIFY( res.in == ix.cend() );
VERIFY( res.out == rx.cend() );
VERIFY( ranges::equal(ix, rx) );
ranges::destroy(rx);
}
}
}
struct X
{
static constexpr int limit = 67;
static inline int copy_construct_count = 0;
static inline int destruct_count = 0;
struct exception {};
bool live = false;
X()
{ live = true; }
X& operator=(const X&) = delete;
X(const X&)
{
live = true;
if (copy_construct_count >= limit)
throw exception{};
copy_construct_count++;
}
~X()
{
VERIFY( live );
live = false;
destruct_count++;
}
};
template<bool test_sized>
void
test02()
{
constexpr int size = 100;
X x[size];
// FIXME: Should be test_input_range?
test_forward_range<X> ix(x);
auto buffer = std::unique_ptr<char[]>(new char[sizeof(X)*size]);
test_forward_range<X> rx((X *)buffer.get(), (X *)buffer.get() + size);
try
{
X::copy_construct_count = 0;
X::destruct_count = 0;
if constexpr (test_sized)
ranges::uninitialized_copy_n(ix.begin(), size, rx.begin(), rx.end());
else
ranges::uninitialized_copy(ix, rx);
VERIFY( false && "exception not thrown" );
}
catch (const X::exception&)
{
VERIFY( X::copy_construct_count == X::limit );
VERIFY( X::destruct_count == X::limit );
}
}
int
main()
{
test01<char>({1,2,3,4,5});
test01<int>({1,2,3,4,5});
test01<long long>({1,2,3,4,5});
test01<float>({1.1,2.1,3.1,4.1});
test01<double>({1.1,2.1,3.1,4.1});
test01<std::vector<char>>({{'a','b'}, {'c','d'}, {'e','f'}});
test01<std::string>({"the", "quick", "brown", "fox"});
test02<false>();
test02<true>();
}
// 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++2a" }
// { dg-do run { target c++2a } }
#include <algorithm>
#include <cstring>
#include <deque>
#include <list>
#include <memory>
#include <span>
#include <string>
#include <vector>
#include <testsuite_hooks.h>
#include <testsuite_iterators.h>
using __gnu_test::test_forward_range;
namespace ranges = std::ranges;
template<typename T>
void
test01()
{
static_assert(std::default_initializable<T>);
static_assert(std::equality_comparable<T>);
for (int k = 0; k < 6; k++)
{
constexpr int size = 1024;
auto buffer = std::unique_ptr<char[]>(new char[sizeof(T)*size]);
std::span<T> rx((T *)buffer.get(), size);
T t;
if constexpr (std::is_fundamental_v<T>)
{
std::memset(&t, 0xCC, sizeof(t));
ranges::fill(rx, t);
}
auto i = rx.cbegin();
if (k == 0)
i = ranges::uninitialized_default_construct(rx.begin(), rx.end());
else if (k == 1)
i = ranges::uninitialized_default_construct(rx);
else if (k == 2)
i = ranges::uninitialized_default_construct_n(rx.begin(), 1024);
else if constexpr (std::is_fundamental_v<T>)
continue;
else if (k == 3)
i = ranges::uninitialized_default_construct(rx.cbegin(), rx.cend());
else if (k == 4)
i = ranges::uninitialized_default_construct(std::as_const(rx));
else if (k == 5)
i = ranges::uninitialized_default_construct_n(rx.cbegin(), 1024);
else
__builtin_abort();
VERIFY( i == rx.cend() );
VERIFY( ranges::find_if(rx, [&t](const T& v) { return t != v; }) == i );
ranges::destroy(rx);
}
}
struct X
{
static constexpr int limit = 67;
static inline int construct_count = 0;
static inline int destruct_count = 0;
struct exception {};
bool live = false;
X()
{
if (construct_count >= limit)
throw exception{};
construct_count++;
live = true;
}
~X()
{
VERIFY( live );
live = false;
destruct_count++;
}
};
template<bool test_sized>
void
test02()
{
constexpr int size = 100;
auto buffer = std::unique_ptr<char[]>(new char[sizeof(X)*size]);
test_forward_range<X> rx((X *)buffer.get(), (X *)buffer.get() + size);
try
{
X::construct_count = 0;
X::destruct_count = 0;
if constexpr (test_sized)
ranges::uninitialized_default_construct_n(rx.begin(), size);
else
ranges::uninitialized_default_construct(rx);
VERIFY( false && "exception not thrown" );
}
catch (const X::exception&)
{
VERIFY( X::construct_count == X::limit );
VERIFY( X::destruct_count == X::limit );
}
}
int
main()
{
test01<char>();
test01<int>();
test01<long long>();
test01<float>();
test01<double>();
test01<std::vector<char>>();
test01<std::string>();
test01<std::deque<double>>();
test01<std::list<std::vector<std::deque<double>>>>();
test01<std::unique_ptr<std::string>>();
test02<false>();
test02<true>();
}
// 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++2a" }
// { dg-do run { target c++2a } }
#include <algorithm>
#include <cstring>
#include <deque>
#include <list>
#include <memory>
#include <span>
#include <string>
#include <vector>
#include <testsuite_hooks.h>
#include <testsuite_iterators.h>
using __gnu_test::test_forward_range;
namespace ranges = std::ranges;
template<typename T>
void
test01(const T& value)
{
static_assert(std::equality_comparable<T>);
for (int k = 0; k < 6; k++)
{
constexpr int size = 1024;
auto buffer = std::unique_ptr<char[]>(new char[sizeof(T)*size]);
std::span<T> rx((T *)buffer.get(), size);
auto i = rx.cbegin();
if (k == 0)
i = ranges::uninitialized_fill(rx.begin(), rx.end(), value);
else if (k == 1)
i = ranges::uninitialized_fill(rx, value);
else if (k == 2)
i = ranges::uninitialized_fill_n(rx.begin(), 1024, value);
else if (k == 3)
i = ranges::uninitialized_fill(rx.cbegin(), rx.cend(), value);
else if (k == 4)
i = ranges::uninitialized_fill(std::as_const(rx), value);
else if (k == 5)
i = ranges::uninitialized_fill_n(rx.cbegin(), 1024, value);
else
__builtin_abort();
VERIFY( i == rx.cend() );
VERIFY( ranges::find_if(rx, [&value](const T& v) { return value != v; }) == i );
ranges::destroy(rx);
}
}
struct X
{
static constexpr int limit = 67;
static inline int construct_count = 0;
static inline int destruct_count = 0;
struct exception {};
bool live = false;
X(int)
{
if (construct_count >= limit)
throw exception{};
construct_count++;
live = true;
}
~X()
{
VERIFY( live );
live = false;
destruct_count++;
}
};
template<bool test_sized>
void
test02()
{
constexpr int size = 100;
auto buffer = std::unique_ptr<char[]>(new char[sizeof(X)*size]);
test_forward_range<X> rx((X *)buffer.get(), (X *)buffer.get() + size);
int value = 5;
try
{
X::construct_count = 0;
X::destruct_count = 0;
if constexpr (test_sized)
ranges::uninitialized_fill_n(rx.begin(), size, value);
else
ranges::uninitialized_fill(rx, value);
VERIFY( false && "exception not thrown" );
}
catch (const X::exception&)
{
VERIFY( X::construct_count == X::limit );
VERIFY( X::destruct_count == X::limit );
}
}
int
main()
{
test01<char>(5);
test01<int>(3);
test01<long long>(17);
test01<float>(2.18);
test01<double>(3.98);
test01<std::vector<char>>({'a', 'b', 'c', 'd'});
test01<std::string>("hello");
test01<std::deque<double>>({1.1,2.1,3.1});
test01<std::list<std::vector<std::deque<double>>>>({{{3.4},{1}},{{7.9}}});
test02<false>();
test02<true>();
}
// 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++2a" }
// { dg-do run { target c++2a } }
#include <algorithm>
#include <cstring>
#include <deque>
#include <list>
#include <memory>
#include <span>
#include <string>
#include <vector>
#include <testsuite_hooks.h>
#include <testsuite_iterators.h>
using __gnu_test::test_input_range;
using __gnu_test::test_forward_range;
namespace ranges = std::ranges;
template<typename T>
void
test01(std::vector<T> ix)
{
static_assert(std::move_constructible<T>);
static_assert(std::equality_comparable<T>);
const auto saved_ix = ix;
for (int k = 0; k < 7; k++)
{
ix = saved_ix;
int size = ix.size();
auto buffer = std::unique_ptr<char[]>(new char[sizeof(T)*size]);
std::span<T> rx((T *)buffer.get(), size);
ranges::uninitialized_move_result res = {ix.cbegin(), rx.cbegin()};
if (k == 0)
res = ranges::uninitialized_move(ix.begin(), ix.end(),
rx.begin(), rx.end());
else if (k == 1)
res = ranges::uninitialized_move(ix, rx);
else if (k == 2)
res = ranges::uninitialized_move_n(ix.begin(), size,
rx.begin(), rx.end());
else if (k == 3)
res = ranges::uninitialized_move(ix.begin(), ix.end(),
rx.cbegin(), rx.cend());
else if (k == 4)
res = ranges::uninitialized_move(ix, std::as_const(rx));
else if (k == 5)
res = ranges::uninitialized_move_n(ix.begin(), size,
rx.cbegin(), rx.cend());
else if (k == 6)
res = ranges::uninitialized_move_n(ix.begin(), size/2,
rx.cbegin(), rx.cend());
else if (k == 7)
res = ranges::uninitialized_move_n(ix.begin(), size,
rx.cbegin(), rx.cbegin()+size/2);
else
__builtin_abort();
if (k == 6 || k == 7)
{
VERIFY( ranges::distance(ix.cbegin(), res.in) == size/2 );
VERIFY( ranges::distance(rx.cbegin(), res.out) == size/2 );
VERIFY( ranges::equal(saved_ix.begin(), saved_ix.begin()+size/2,
rx.begin(), rx.begin()+size/2) );
ranges::destroy(rx.begin(), rx.begin()+size/2);
}
else
{
VERIFY( res.in == ix.cend() );
VERIFY( res.out == rx.cend() );
VERIFY( ranges::equal(saved_ix, rx) );
ranges::destroy(rx);
}
}
}
struct X
{
static constexpr int limit = 67;
static inline int move_construct_count = 0;
static inline int destruct_count = 0;
struct exception {};
bool live = false;
bool moved_from = false;
X()
{ live = true; moved_from = false; }
X& operator=(const X&) = delete;
X(const X&) = delete;
X&& operator=(X&&) = delete;
X(X&& other)
{
VERIFY( !other.moved_from );
other.moved_from = true;
live = true;
if (move_construct_count >= limit)
throw exception{};
move_construct_count++;
}
~X()
{
VERIFY( live );
live = false;
destruct_count++;
}
};
template<bool test_sized>
void
test02()
{
constexpr int size = 100;
X x[size];
// FIXME: Should be test_input_range?
test_forward_range<X> ix(x);
auto buffer = std::unique_ptr<char[]>(new char[sizeof(X)*size]);
test_forward_range<X> rx((X *)buffer.get(), (X *)buffer.get() + size);
try
{
X::move_construct_count = 0;
X::destruct_count = 0;
if constexpr (test_sized)
ranges::uninitialized_move_n(ix.begin(), size, rx.begin(), rx.end());
else
ranges::uninitialized_move(ix, rx);
VERIFY( false && "exception not thrown" );
}
catch (const X::exception&)
{
VERIFY( X::move_construct_count == X::limit );
VERIFY( X::destruct_count == X::limit );
}
}
int
main()
{
test01<char>({1,2,3,4,5});
test01<int>({1,2,3,4,5});
test01<long long>({1,2,3,4,5});
test01<float>({1.1,2.1,3.1,4.1});
test01<double>({1.1,2.1,3.1,4.1});
test01<std::vector<char>>({{'a','b'}, {'c','d'}, {'e','f'}});
test01<std::string>({"the", "quick", "brown", "fox"});
test02<false>();
test02<true>();
}
// 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++2a" }
// { dg-do run { target c++2a } }
#include <algorithm>
#include <cstring>
#include <deque>
#include <list>
#include <memory>
#include <span>
#include <string>
#include <vector>
#include <testsuite_hooks.h>
#include <testsuite_iterators.h>
using __gnu_test::test_forward_range;
namespace ranges = std::ranges;
template<typename T>
void
test01()
{
static_assert(std::default_initializable<T>);
static_assert(std::equality_comparable<T>);
for (int k = 0; k < 6; k++)
{
constexpr int size = 1024;
auto buffer = std::unique_ptr<char[]>(new char[sizeof(T)*size]);
std::span<T> rx((T *)buffer.get(), size);
T t{};
auto i = rx.cbegin();
if (k == 0)
i = ranges::uninitialized_value_construct(rx.begin(), rx.end());
else if (k == 1)
i = ranges::uninitialized_value_construct(rx);
else if (k == 2)
i = ranges::uninitialized_value_construct_n(rx.begin(), 1024);
else if (k == 3)
i = ranges::uninitialized_value_construct(rx.cbegin(), rx.cend());
else if (k == 4)
i = ranges::uninitialized_value_construct(std::as_const(rx));
else if (k == 5)
i = ranges::uninitialized_value_construct_n(rx.cbegin(), 1024);
else
__builtin_abort();
VERIFY( i == rx.cend() );
VERIFY( ranges::find_if(rx, [&t](const T& v) { return t != v; }) == i );
ranges::destroy(rx);
}
}
struct X
{
static constexpr int limit = 67;
static inline int construct_count = 0;
static inline int destruct_count = 0;
struct exception {};
bool live = false;
X()
{
if (construct_count >= limit)
throw exception{};
construct_count++;
live = true;
}
~X()
{
VERIFY( live );
live = false;
destruct_count++;
}
};
template<bool test_sized>
void
test02()
{
constexpr int size = 100;
auto buffer = std::unique_ptr<char[]>(new char[sizeof(X)*size]);
test_forward_range<X> rx((X *)buffer.get(), (X *)buffer.get() + size);
try
{
X::construct_count = 0;
X::destruct_count = 0;
if constexpr (test_sized)
ranges::uninitialized_value_construct_n(rx.begin(), size);
else
ranges::uninitialized_value_construct(rx);
VERIFY( false && "exception not thrown" );
}
catch (const X::exception&)
{
VERIFY( X::construct_count == X::limit );
VERIFY( X::destruct_count == X::limit );
}
}
int
main()
{
test01<char>();
test01<int>();
test01<long long>();
test01<float>();
test01<double>();
test01<std::vector<char>>();
test01<std::string>();
test01<std::deque<double>>();
test01<std::list<std::vector<std::deque<double>>>>();
test01<std::unique_ptr<std::string>>();
test02<false>();
test02<true>();
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment