C++ Logo

std-proposals

Advanced search

Re: [std-proposals] "once" keyword

From: Sebastian Wittmeier <wittmeier_at_[hidden]>
Date: Wed, 24 May 2023 18:26:41 +0200
Perhaps something related to the proposed and actively discussed do expressions? Or they provide already enough functionality for your use case?   https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2023/p2806r1.html https://github.com/cplusplus/papers/issues/1462       do {     if (Failed(Condition0))         do_return;     if (Failed(Condition1))         do_return;     Code_to_execute; }   You can even 'do':   if (do {     if (Failed(Condition0))         do_return false;     if (Succeeded(ConditionShortCut))         do_return true;     if (Failed(Condition1))         do_return false;     return true; })     Code_to_execute;   do blocks are compatible with return, break, continue, throw, co_return, etc. (and to some degree goto)   (do_return is currently planned instead of do return).     -----Ursprüngliche Nachricht----- Von:LUCE Jean-Sébastien via Std-Proposals <std-proposals_at_[hidden]> Gesendet:Mi 24.05.2023 14:58 Betreff:[std-proposals] "once" keyword An:std-proposals_at_[hidden]; CC:LUCE Jean-Sébastien <jseb.luce_at_[hidden]>; Hello It is often useful to support multiple cancel conditions, like 'return' in a function, but without having to isolate the code in such a function (which is called from a single point). I'm used to write code like "  do {      if (Failed(Condition0))         break;      if (Failed(Condition1))         break;      Code_to_execute; } while (0); " or  "  while (Condition0) {      if (Failed(Condition1))         break;      Code_to_execute;      break; }; " the example above looks trivial and could be replaced by  if (Succeeded(Condition0) &&  Succeeded(Condition1)) {     Code_to_execute; } Yet with more complex condition codes, writing conditions per line is way more readable. The syntaxe is still "hacked" someway, since "continue" could be used wrongly, or final "break" may be forgotten. I would like "once" behave like do { } while(0);, where only "break" (and not "continue") would have a meaning. for example above could be converted as  "  once {      if (Failed(Condition0))         break;      if (Failed(Condition1))         break;      Code_to_execute; } " "  once  (Condition0) {      if (Failed(Condition1))         break;      Code_to_execute; } " "once" could be followed or not by a first condition. Thanks -- Std-Proposals mailing list Std-Proposals_at_[hidden] https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals  

Received on 2023-05-24 16:26:42