3 ms·
I do this in c++ quite often with short lambdas auto not_null = [&]{ return (bool)p; }; auto not_space = [&]{ return (*p != ' '); }; if (not_null() && n
by injidup 4y ago
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 https://godbolt.org/z/3cczcnK4s
- garaetjjte 4y ago>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.