C++ Logo


Advanced search

Re: [std-proposals] "once" keyword

From: David Brown <david_at_[hidden]>
Date: Wed, 24 May 2023 17:57:26 +0200
On 24/05/2023 17:14, Jason McKesson via Std-Proposals wrote:
> On Wed, May 24, 2023 at 10:51 AM Arthur O'Dwyer via Std-Proposals
> <std-proposals_at_[hidden]> wrote:
>> On Wed, May 24, 2023 at 10:36 AM LUCE Jean-Sébastien via Std-Proposals <std-proposals_at_[hidden]> wrote:
>>> Yes this was why I talked about another keyword, changing "if" behaviour (with break) would create big mess with all existant c++ code.
>>> I agree with most of our comments, yet I still found "goto" syntax not ideal.
>>> And found it strange to have no "return" equivalent in a sub part of a function.
>> You've hit the nail on the head. One of the big ideas of the "structured programming" revolution of the '60s–'70s was functional decomposition: the idea that if your program is doing more than one "thing," you should break it down into smaller functions where each function accomplishes just one "thing." Every time you find yourself needing "the equivalent of `return` but for a smaller part of a foo," structured programming's answer is always going to be: Break that foo down into smaller functions, and then use `return`.
>> `return` is the `return` you're looking for. :)
> And with immediately-invoked lambdas, you don't even need to break the
> code out of the function to do that. Sure, the syntax is slightly odd,
> having to prefix the code with `[&]{`, and suffix it with `}();`, but
> overall, it's not much syntax.
> Indeed, there might be a case to be made for having special syntax for
> creating an immediately-invoked lambda expression (always capturing
> `&` and never taking parameters) that puts the syntax at the front of
> the lambda rather than relying on the user to notice the `()` at the
> end.

#define λ [&]
#define Ω ()

int foo(int x) {
     int y = x;

     λ {
         if (y == 1) return;
     } Ω;

     return y;

I did think that "}();" might be some kind of smiley, but you are not
allowed 😦 as a macro identifier :-(

I guess something like "[{ ... }]" might be a bit more sensible.

Received on 2023-05-24 15:57:35