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