Fix counter postfix++ bug
This commit is contained in:
parent
f9215e1b63
commit
ceab9a14cc
|
@ -6,25 +6,45 @@
|
||||||
template <typename T>
|
template <typename T>
|
||||||
struct Counter
|
struct Counter
|
||||||
{
|
{
|
||||||
using value_type = T;
|
using difference_type = std::ptrdiff_t;
|
||||||
using difference_type = std::ptrdiff_t;
|
|
||||||
using pointer = T*;
|
|
||||||
using reference = T&;
|
|
||||||
using iterator_category = std::output_iterator_tag;
|
|
||||||
|
|
||||||
struct EmptyRef {
|
struct EmptyRef {
|
||||||
auto operator=(T const&) const -> void { }
|
auto operator=(T const&) const -> void {}
|
||||||
|
auto operator=(T && ) const -> void {}
|
||||||
};
|
};
|
||||||
|
|
||||||
std::size_t n;
|
std::size_t n;
|
||||||
|
|
||||||
auto operator++() -> Counter& { n++; return *this; }
|
Counter() : n{0} {}
|
||||||
auto operator++(int) -> Counter { Counter temp; n++; return temp; }
|
Counter(std::size_t n) : n{n} {}
|
||||||
auto operator*() const -> EmptyRef { return EmptyRef{}; }
|
|
||||||
auto operator-(Counter const& rhs) const -> std::ptrdiff_t {
|
auto operator++() -> Counter&
|
||||||
return std::ptrdiff_t{n} - std::ptrdiff_t{rhs.n};
|
{
|
||||||
|
n++;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto operator++(int) -> Counter
|
||||||
|
{
|
||||||
|
auto temp = *this;
|
||||||
|
n++;
|
||||||
|
return temp;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto operator*() const -> EmptyRef
|
||||||
|
{
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
auto operator-(Counter rhs) const -> difference_type
|
||||||
|
{
|
||||||
|
return difference_type{n} - difference_type{rhs};
|
||||||
|
}
|
||||||
|
|
||||||
|
operator std::size_t() const
|
||||||
|
{
|
||||||
|
return n;
|
||||||
}
|
}
|
||||||
operator std::size_t() const { return n; }
|
|
||||||
};
|
};
|
||||||
|
|
||||||
static_assert(std::output_iterator<Counter<int>, int>);
|
static_assert(std::output_iterator<Counter<int>, int>);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user