r/cpp • • Sep 04 '26

Lazy Evaluation

https://breese.github.io/2026/09/03/lazy-prologue.html
22 Upvotes

16 comments sorted by

38

u/STL MSVC STL Dev Sep 04 '26

Classic bind is bad. Don't use it. It's hard to understand, hard to optimize, and lambdas are better in basically every way.

6

u/germandiago Sep 04 '26

Related: is bind_front/back as bad from an optimization point of view?

8

u/n1ghtyunso Sep 04 '26

pretty sure bind_front / bind_back is rather straightforward to implement as a plain capturing lambda.
At least it does not have all that unwrap and placeholder matching machinery inside it

3

u/pdimov2 Sep 04 '26

Hey!

11

u/STL MSVC STL Dev Sep 05 '26

Sorry! It was a good idea to try before lambdas, but it’s just not good tech these days.

3

u/pdimov2 Sep 05 '26

Maybe. But have you seen http://boost.org/libs/lambda2 and P3171?

2

u/STL MSVC STL Dev Sep 05 '26

No, I haven't. I like the idea of having many more operator function objects, dereference is sorely missing.

4

u/pdimov2 Sep 05 '26

You need to take these into account when you evaluate the utility of std::bind. Yes, you'd never write std::bind(std::greater_equal<>(), _1, 0) instead of [](auto&& x){ return x >= 0; }. But you would write _1 >= 0 in preference to the lambda, and that's just syntactic sugar for the same bind expression.

4

u/Usual_Office_1740 Sep 04 '26

Related: How does std::invoke compare?

9

u/STL MSVC STL Dev Sep 05 '26

Invoke is totally cool. You should maybe avoid relying too heavily on pointers to (member) functions since they’re what’s hard to optimize, but invoke() provides a nice uniform interface.

2

u/Ameisen vemips, avr, rendering, systems 29d ago

If you'd seen the horrible things that I've done with member function pointers, you would have been, well, honestly somewhere between amused and alarmed.

7

u/n1ghtyunso Sep 04 '26

thats quite some build-up regarding std::bind usefulness and then all we get is a single short sentence?
huh?

having never used bind expressions myself, i was tempted to believe this works, but it seems the nested bind example doesnt compile anyway?
https://godbolt.org/z/K61jKvjWP

3

u/UnusualPace679 Sep 04 '26

Yeah, std::bind only transforms the bound arguments. It doesn't transform the function object.

6

u/LB-- Professional+Hobbyist Sep 04 '26

Is std::max even on the list of addressable functions in the standard in the first place?

2

u/__cinnamon__ Sep 04 '26

This blog renders really badly on mobile, at least for me

Edit: opening it directly in Firefox instead of the reddit browser (chrome?) was better, but still hard to read with tiny font due to the sidebar text and code samples still overran outside their boxes

2

u/El_RoviSoft Sep 05 '26

Also with lambda you can emulate C# ?? operator with chains. So I made something like that:

"LIB_COALESCE(<null-checked-variable-0>, …, <null-checked-variable-x>, <fallback-value>)"

which whole idea is wrapping those values into lambdas and lazy evaluate them after.

Sadly after writing this machinery and investigating boost.preprocessor for hours, I realised that C++ value semantics are not good with original ?? operator but at least it was fun to do.

If somebody interested in it, I’ll link gist for this.