From ceab9a14ccb036cb983037ed9ddf6ee4d5e955ab Mon Sep 17 00:00:00 2001 From: Eric Mertens Date: Mon, 4 Dec 2023 17:28:16 -0800 Subject: [PATCH] Fix counter postfix++ bug --- lib/include/aocpp/Counter.hpp | 46 +++++++++++++++++++++++++---------- 1 file changed, 33 insertions(+), 13 deletions(-) diff --git a/lib/include/aocpp/Counter.hpp b/lib/include/aocpp/Counter.hpp index e257db5..a23dace 100644 --- a/lib/include/aocpp/Counter.hpp +++ b/lib/include/aocpp/Counter.hpp @@ -6,25 +6,45 @@ template 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, int>);