Date: Fri, 27 Dec 2019 13:05:52 +0100
On 2019-12-27 at 12:31, Михаил Найденов via Std-Proposals wrote:
> 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] <mailto: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 statements
> step3: return to step1
>
> 2. do:
> step1: Execute statements
> step2: Evaluate condition. Return to srep1 if the condition is true
>
> 3 for:
> step1: create a counter
> step2: Evaluate condition. Continue to step3 if the condition is true
> step3: Execute statements
> step4: 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 counter
> step2: execute statements
> step3: 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.
>
Compilers know existing loop forms well enough that efficiency is a very
weak argument. They already optimize pretty well. :-)
For example, it is not uncommon to see the while-loop rewritten as
while:
step0: goto step2
step1: Execute statements
step2: Evaluate condition. Return to step1 if the condition is true
which makes it very similar to the do-while.
It looks to me that the programmer could very often transform a dofor
into a regular for by just adding a +1 or -1 to the initial value of the
loop variable. I wouldn't be surprised at all if a compiler sees that
and transforms the two versions into the same assembly code.
So, doubtful if a language change (rather expensive!) would bring us
anything but a very minimal gain. So perhaps not worth it?
Bo Persson
> 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] <mailto: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 statements
> step3: return to step1
>
> 2. do:
> step1: Execute statements
> step2: Evaluate condition. Return to srep1 if the condition is true
>
> 3 for:
> step1: create a counter
> step2: Evaluate condition. Continue to step3 if the condition is true
> step3: Execute statements
> step4: 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 counter
> step2: execute statements
> step3: 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.
>
Compilers know existing loop forms well enough that efficiency is a very
weak argument. They already optimize pretty well. :-)
For example, it is not uncommon to see the while-loop rewritten as
while:
step0: goto step2
step1: Execute statements
step2: Evaluate condition. Return to step1 if the condition is true
which makes it very similar to the do-while.
It looks to me that the programmer could very often transform a dofor
into a regular for by just adding a +1 or -1 to the initial value of the
loop variable. I wouldn't be surprised at all if a compiler sees that
and transforms the two versions into the same assembly code.
So, doubtful if a language change (rather expensive!) would bring us
anything but a very minimal gain. So perhaps not worth it?
Bo Persson
Received on 2019-12-27 06:08:32