C++ solution using a Functor