Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

P0896R4 changes to insert iterators #589

Merged
merged 6 commits into from
Mar 10, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 28 additions & 7 deletions stl/inc/iterator
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,19 @@ class back_insert_iterator { // wrap pushes to back of container as output itera
public:
using iterator_category = output_iterator_tag;
using value_type = void;
using difference_type = void;
using pointer = void;
using reference = void;

using container_type = _Container;

#ifdef __cpp_lib_concepts
using difference_type = ptrdiff_t;

constexpr back_insert_iterator() noexcept = default;
#else // ^^^ __cpp_lib_concepts / !__cpp_lib_concepts vvv
using difference_type = void;
#endif // __cpp_lib_concepts

explicit back_insert_iterator(_Container& _Cont) noexcept /* strengthened */ : container(_STD addressof(_Cont)) {}

back_insert_iterator& operator=(const typename _Container::value_type& _Val) {
Expand All @@ -56,7 +63,7 @@ public:
}

protected:
_Container* container; // pointer to container
_Container* container = nullptr;
};

// FUNCTION TEMPLATE back_inserter
Expand All @@ -72,12 +79,19 @@ class front_insert_iterator { // wrap pushes to front of container as output ite
public:
using iterator_category = output_iterator_tag;
using value_type = void;
using difference_type = void;
using pointer = void;
using reference = void;

using container_type = _Container;

#ifdef __cpp_lib_concepts
using difference_type = ptrdiff_t;

constexpr front_insert_iterator() noexcept = default;
#else // ^^^ __cpp_lib_concepts / !__cpp_lib_concepts vvv
using difference_type = void;
#endif // __cpp_lib_concepts

explicit front_insert_iterator(_Container& _Cont) : container(_STD addressof(_Cont)) {}

front_insert_iterator& operator=(const typename _Container::value_type& _Val) { // push value into container
Expand All @@ -103,7 +117,7 @@ public:
}

protected:
_Container* container; // pointer to container
_Container* container = nullptr;
};

// FUNCTION TEMPLATE front_inserter
Expand All @@ -119,12 +133,19 @@ class insert_iterator { // wrap inserts into container as output iterator
public:
using iterator_category = output_iterator_tag;
using value_type = void;
using difference_type = void;
using pointer = void;
using reference = void;

using container_type = _Container;

#ifdef __cpp_lib_concepts
using difference_type = ptrdiff_t;

insert_iterator() = default;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

out of curiosity: Why isn't this one constexpr (in the standard)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have absolutely no idea. (To be honest, I don't understand why we ever put an explicit constexpr on a defaulted default constructor.) These default constructors came from P0896, which crossed with P1032 that added constexpr to all of the members of the insert iterators, and apparently the editors decided to put constexpr on some subset of the default constructors for reasons not apparent to me.

#else // ^^^ __cpp_lib_concepts / !__cpp_lib_concepts vvv
using difference_type = void;
#endif // __cpp_lib_concepts

insert_iterator(_Container& _Cont, typename _Container::iterator _Where)
: container(_STD addressof(_Cont)), iter(_Where) {}

Expand Down Expand Up @@ -155,8 +176,8 @@ public:
}

protected:
_Container* container; // pointer to container
typename _Container::iterator iter; // iterator into container
_Container* container = nullptr;
CaseyCarter marked this conversation as resolved.
Show resolved Hide resolved
typename _Container::iterator iter = {};
CaseyCarter marked this conversation as resolved.
Show resolved Hide resolved
};

// FUNCTION TEMPLATE inserter
Expand Down
25 changes: 25 additions & 0 deletions tests/std/tests/P0896R4_ranges_iterator_machinery/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
#include <compare>
#include <concepts>
#include <iterator>
#include <list>
#include <type_traits>
#include <utility>
#include <vector>

#define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__)

Expand Down Expand Up @@ -2770,6 +2772,29 @@ namespace iter_ops {
}
} // namespace iter_ops

namespace insert_iterators {
template <class Container>
constexpr bool test() {
using std::back_insert_iterator, std::front_insert_iterator, std::insert_iterator;
using std::default_initializable, std::is_nothrow_default_constructible_v, std::iter_difference_t,
std::ptrdiff_t, std::same_as;

STATIC_ASSERT(default_initializable<back_insert_iterator<Container>>);
CaseyCarter marked this conversation as resolved.
Show resolved Hide resolved
STATIC_ASSERT(is_nothrow_default_constructible_v<back_insert_iterator<Container>>);
STATIC_ASSERT(default_initializable<front_insert_iterator<Container>>);
STATIC_ASSERT(is_nothrow_default_constructible_v<front_insert_iterator<Container>>);
STATIC_ASSERT(default_initializable<insert_iterator<Container>>);
STATIC_ASSERT(same_as<iter_difference_t<back_insert_iterator<Container>>, ptrdiff_t>);
STATIC_ASSERT(same_as<iter_difference_t<front_insert_iterator<Container>>, ptrdiff_t>);
STATIC_ASSERT(same_as<iter_difference_t<insert_iterator<Container>>, ptrdiff_t>);

return true;
}

STATIC_ASSERT(test<std::list<double>>());
STATIC_ASSERT(test<std::vector<int>>());
} // namespace insert_iterators

int main() {
iterator_cust_swap_test::test();
iter_ops::test();
Expand Down