Skip to content

Commit 81e4a93

Browse files
committed
Update map_iteratort to use a shared_ptr for current
This allows mapping into move only types. This was not previously possible, because the copy constructor of `map_iteratort` copied the value pointed to by its unique pointer in its `current` field. This can also be considered to be more correct, because copying an iterator should be expected to yield two copies, which point to the same value, rather than two copies, which point to two separate values.
1 parent 16bc92f commit 81e4a93

File tree

1 file changed

+3
-12
lines changed

1 file changed

+3
-12
lines changed

src/util/range.h

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class map_iteratort
4848
PRECONDITION(underlying != underlying_end);
4949
++underlying;
5050
if(underlying != underlying_end)
51-
current = util_make_unique<outputt>((*f)(*underlying));
51+
current = std::make_shared<outputt>((*f)(*underlying));
5252
return *this;
5353
}
5454

@@ -80,16 +80,7 @@ class map_iteratort
8080
f(std::move(f))
8181
{
8282
if(this->underlying != this->underlying_end)
83-
current = util_make_unique<outputt>((*this->f)(*this->underlying));
84-
}
85-
86-
map_iteratort(const map_iteratort &other)
87-
: underlying(other.underlying),
88-
underlying_end(other.underlying_end),
89-
f(other.f)
90-
{
91-
if(other.current != nullptr)
92-
current = util_make_unique<outputt>(*other.current.get());
83+
current = std::make_shared<outputt>((*this->f)(*this->underlying));
9384
}
9485

9586
private:
@@ -101,7 +92,7 @@ class map_iteratort
10192

10293
/// Stores the result of \c f at the current position of the iterator.
10394
/// Equals nullptr if the iterator reached \c underlying_end.
104-
std::unique_ptr<outputt> current = nullptr;
95+
std::shared_ptr<outputt> current = nullptr;
10596
};
10697

10798
/// Iterator which only stops at elements for which some given function \c f is

0 commit comments

Comments
 (0)