Using std 11 algoritms

calling ‘for_each’ algorithm from STL , last parameter Function f, I assume this must be a function declared by class Function that is Function.h, however, none of templates listed there can be passed, I realize Function.h must be declarations only, then where are the definitions? More importantly, How can you pass a function to the 3rd parameter of ‘for_each’, also is problem for other algorithms ???

Google Search -> std::for_each example

Hi,
as you can see from its docs std::for_each takes any function or object that has the signature void fun(const Type &a), where Type is the type of the elements of the container that you are iterating on. See e.g. @Wile_E_Coyote’s link for example usages.

This is completely unrelated to ROOT though.
In particular, the Function.h header is an internal llvm header that is not supposed to be included by users. You can tell by the fact that even though Function.h is present in ROOT’s sources, you won’t find it in you ROOT installation directory!
ROOT also provides a TFunction.h header which should define a function object, but TFunction predates std::for_each and cannot be called like a function at all.

It also takes lambdas, which conveniently can be created in place. You can trick std::for_each to take a TFunction by capturing it:

TF1 myfunc = /* ... */;
std::for_each(/* ... */, [&](Type& t) { 
    myfunc(t);

});
1 Like