C++ Logo

std-proposals

Advanced search

Re: dofor loop

From: Menashe Rosemberg <rosemberg_at_[hidden]>
Date: Fri, 27 Dec 2019 13:28:21 +0000 (UTC)
I can't consider the basic loops 'out of fashion' because they are large used and they are the simple and best way to interate over primitive elements or keep a flow control.
Just for example, I have a template function where I receive an array and I want to print the number of index elements
 //1. While -----------------------------------------------------------------------------------------------------------
            template <typename obj, size_t S, enable_if_t <S != 0, size_t> = 1>
            void WhileDescending(const array<obj, S>& Obj) {
                 size_t Size = Obj.size();
                 while (Size) {
                       --Size;
                       cout << Size;
                 }
            }
//2. do --------------------------------------------------------------------------------------------------------------- template <typename obj, size_t S, enable_if_t <S != 0, size_t> = 1>
            void DoDescending(const array<obj, S>& Obj) {
                 size_t Size = Obj.size();
                 do {
                     --Size;
                     cout << Size;
                 } while (Size);
            }
//3. for ---------------------------------------------------------------------------------------------------------------- template <typename obj, size_t S, enable_if_t <S != 0, size_t> = 1>
            void ForDescending(const array<obj, S>& Obj) {
                 for (size_t Size = Obj.size() - 1; Size != numeric_limits<size_t>::max(); --Size)
                     cout << Size;
            }

//The proposal 'dofor' --------------------------------------------------------------------------------------------- template <typename obj, size_t S, enable_if_t <S != 0, size_t> = 1>
            void DoForDescending(const array<obj, S>& Obj) {
                 dofor (size_t Size = Obj.size(); --Size; Size >= 0)
                       cout << Size;
            }
Also, we can say the for loop is to while loop with a counter. With 'dofor' loop we can give the same for do..while loop.

   Em sexta-feira, 27 de dezembro de 2019 13:31:57 GMT+2, Михаил Найденов via Std-Proposals <std-proposals_at_[hidden]> escreveu:
 
 Note that raw loops are out of fashion right now. Also, there are arguably too many iteration statements already, considering all can be expressed with one or two of the existing ones.Can you give some compelling examples.
On Fri, Dec 27, 2019 at 1:04 PM Menashe Rosemberg via Std-Proposals <std-proposals_at_[hidden]> wrote:

Hi,

The loops we have in c++ are loop while, for and do...while. How they work is pretty know, but I will describe them shortly (in their general use) to better demonstrate my proposal.
1. while:step1: Evaluate condition. Continue to step2 if the condition is true
step2: Execute statementsstep3: return to step1
2. do:step1: Execute statementsstep2: Evaluate condition. Return to srep1 if the condition is true
3 for:step1: create a counterstep2: Evaluate condition. Continue to step3 if the condition is truestep3: Execute statementsstep4: Execute an mathematics operation (with the counter) and return to step2
What I see is we have a lack of one more that may help to make the c++ language more robust and elegant and that is a union of loop do and for to form a 'dofor' loop. It may have the same syntax of for loop but the condition is evaluated in the end not in the start of loop as do loop does.
dofor loop:step1: create a counterstep2: execute statementsstep3: Execute a mathematics operation (with the counter)step4: return to step2 if condition is true
The syntax could be similar to for like that:
dofor ( <variable declaration>; <mathematics operation>; <condition>) { statements...
}

Also the step3 and step4 may be inlined on the assemble level and make this loop very efficient.

Best regards,TheArquitect
-- 
Std-Proposals mailing list
Std-Proposals_at_[hidden]
https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
-- 
Std-Proposals mailing list
Std-Proposals_at_[hidden]
https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
  

Received on 2019-12-27 07:30:49