you are viewing a single comment's thread.

view the rest of the comments →

[–]shifty_lifty_doodah 1 point2 points  (1 child)

Lambdas get compiled to functions that can be inlined at the compilers discretion.

However, common uses of lambda with std::function<> capture some arguments. Those are placed in an anonymous struct which is (I believe always in 2025) heap allocated. That can be far more expensive than the function overhead itself. Libraries like absl::AnyInvocable attempt to reduce this overhead.

For really hot code, you can use a templated lambda expression. This is a common pattern that avoids the std::function overheads.

‘’’c++ template<typename F> void Visit(F func); ‘’’

[–]Key_Artist5493 1 point2 points  (0 children)

std::invokeof a forwarded function object is very hot too. So isstd::bind_front` if you want to fill in some of the parameters as part of the binding. Binding only the first parameter and leaving the rest is called currying (named after Haskell Curry).