c callback vs vector return in a data structure traversal method?

In this case I would use the callback approach, but I would remove the std::function and replace it with a template type. std::function uses type erasure and that requires dynamic allocation and a little bit of overhead. A function template like

template<typename Callback>
void query(T query, const Callback& cb) {
    for (auto &k : v)
        if (match(query, k))
            cb(k);
}

will remove that overhead and still let you pass any function object you want to the function.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top