News for hackers. Stuff that matters.
Re:Named Booleans prevent C++ bugs and save you time by injidup on Saturday February 18, 2023 @01:19PM (#34849430)
I do this in c++ quite often with short lambdas

  auto not_null = [&]{ return (bool)p; };
  auto not_space = [&]{ return (*p != ' '); };

  if (not_null() && not_space()) {
It's not quite as ergonomic but it works.

As an aside. I was curious if the compiler will optimize in the short circuit even if you extract the eagerly evaluated named booleans. However I was pretty sure the rules of c++ are against you and tests show this to be the case

But if you simulate the alias keyword with lambdas then the result after optimization are the same

https://godbolt.org/z/3cczcnK4s

Re:Named Booleans prevent C++ bugs and save you time by garaetjjte on Sunday February 19, 2023 @03:07PM (#34861299)
>I was curious if the compiler will optimize in the short circuit even if you extract the eagerly

It will optimize, but not necessarily what you wanted... Because null dereference is UB (with default compiler flags), it's allowed to elide null check.