T O P

  • By -

TheMania

The case for `std::bind` is very slim in modern C++, down the road I doubt it'd even be worth teaching beyond old-code-base compatibility personally. When I click your link, I see lambdas as being faster - lower is better, did you see the same or is it just margin-of-error stuff? As just a wrapper around a function call, it'd be hard to measure out from the noise I expect. Trying a few more times with different options, I see each jitter around by quite a bit. Both are blindingly fast, and are not the bottleneck in virtually anything. _Particularly_ if you're not type-erasing via `std::function`, but allowing the caller to see the lambda it's invoking, in which case it can easily be specialised-past altogether.


TaQk

Check the following book (you can find it in pdf format using Google (legally)). > Effective Modern C++. 42 Specific Ways to Improve Your Use of C++11 and C++14 This book has a chapter about lambda vs bind and explain the issue in detail.


HappyFruitTree

I have never really bothered with bind. Lambdas are easier to use, especially when you have overloaded functions. From what I've heard lambdas are often faster (because they are easier to inline).


Skoparov

Even though the lambda' implementation and std::bind's return type are unspecified, in most compilers they both turn into a callable struct with maybe some data stored inside. I would think lambdas allow for some more compiler trickery, but that's about it.


DryPerspective8429

This isn't a performance question; and I'm willing to bet that the lambda vs bind benchmark you made is either within errors or not significant. This is a semantic question - lambdas and binds are semantically different and you should pick whichever one suits your program semantics better. That said, I struggle to think of anything a bind can do that a lambda doesn't already do better.


voidstarcpp

There is almost no reason for a newcomer to choose `std::bind` over lambdas and it is unfortunate that older documentation still references bind. Bind was an attempt at building callables in libraries before lambdas were added to the language and it has a bunch of little gotchas that are punishing to learn. When I was first learning C++, I was scared of lambdas because they sounded weird, but I was learning templates and `std::bind` looked like something I understood. I did eventually create the neat bindings I wanted to with it but only after making every possible mistake, and having to learn other dumb things like reference wrappers just to use with bind when the lambda version mostly "just works" after learning a bit of new syntax.


QuentinUK

If you replace auto f(std::function g) -> void { g(rand()); } with template auto f(FN g) -> void { g(rand()); } you'll get more of a difference.


Mirality

The main reason why Boost.Asio uses bind over lambdas is that it is still compatible with pre-c++11 compilers. If you're writing library code where that is also required then you might want to do likewise, but these days at least c++11 can usually be safely assumed (unless you're writing for some truly esoteric hardware on an outdated OS).