In the following example, I would like a traverse method that receives a callback. This example works perfectly as soon as I don't capture anything [] because the lambda can be reduced into a function pointer. However, in this particular case, I would like to access sum.
struct Collection {
    int array[10];
    void traverse(void (*cb)(int &n)) {
        for(int &i : array)
            cb(i);
    }
    int sum() {
        int sum = 0;
        traverse([&](int &i) {
            sum += i;
        });
    }
}
What is the proper way (without using any templates) to solve this? A solution is to use a typename template as follows. But in this case, you lack visibility on what traverse gives in each iteration (an int):
template <typename F>
void traverse(F cb) {
    for(int &i : array)
        cb(i);
}
 
     
     
    