-
-
Save fabiogaluppo/c128606504709b1c7566 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template<typename T> | |
struct accumulator_with_predicate final | |
{ | |
using UnaryPredicate = std::function<bool(T)>; | |
explicit accumulator_with_predicate(T init, UnaryPredicate pred) | |
: acc(init), pred(pred) {} | |
accumulator_with_predicate& operator+(const T& val) | |
{ | |
if (pred(val)) acc += val; | |
return *this; | |
} | |
operator T() const { return acc; } | |
accumulator_with_predicate() = delete; //default-constructible | |
accumulator_with_predicate(const accumulator_with_predicate&) = default; //copy-constructible | |
accumulator_with_predicate& operator=(const accumulator_with_predicate&) = default; //copy-assignable | |
~accumulator_with_predicate() = default; //destructor | |
private: | |
T acc; | |
UnaryPredicate pred; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment