Range-fors with intialisers too?

84 views
Skip to first unread message

Thiago Macieira

unread,
Jun 25, 2016, 12:09:34 AM6/25/16
to std-pr...@isocpp.org
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0305r0.html
(If statement with initializer) introduces the following syntax:

if ( for-init-statement condition ) statement

The paper talks about using it for switch too but says that it is not
proposing then. Let's say we accept that the benefit would be small.

Regular while and for loops already have init statements.

The one missing is the range for. Could we have the following?

for ( for-init-statement for-range-declaration : for-range-initializer)

That would allow code like:

for (auto list = get_list(); auto entry : list) {
use_both(entry, list);
}

Provided this new extension is worded properly, it would also solve the
problem of the dangling temporaries that the current ranged for calls for.

--
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
Software Architect - Intel Open Source Technology Center

Nicol Bolas

unread,
Jun 25, 2016, 12:40:32 AM6/25/16
to ISO C++ Standard - Future Proposals
On Saturday, June 25, 2016 at 12:09:34 AM UTC-4, Thiago Macieira wrote:
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0305r0.html
 (If statement with initializer) introduces the following syntax:

        if ( for-init-statement condition ) statement

The paper talks about using it for switch too but says that it is not
proposing then. Let's say we accept that the benefit would be small.

Regular while and for loops already have init statements.

Since when do while loops have init statements? Or are you just talking about `for` loops that don't have an increment statement?

Thiago Macieira

unread,
Jun 25, 2016, 12:52:19 AM6/25/16
to std-pr...@isocpp.org
I was thinking of how you can declare variables in them, but I didn't think it
through. It's the same syntax as plain old if, which means that anything that
applies to if should apply to while too.

Thiago Macieira

unread,
Jun 25, 2016, 12:53:37 AM6/25/16
to std-pr...@isocpp.org
On sexta-feira, 24 de junho de 2016 21:52:15 PDT Thiago Macieira wrote:
> On sexta-feira, 24 de junho de 2016 21:40:31 PDT Nicol Bolas wrote:
> > On Saturday, June 25, 2016 at 12:09:34 AM UTC-4, Thiago Macieira wrote:
> > > http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0305r0.html
> > >
> > > (If statement with initializer) introduces the following syntax:
> > > if ( for-init-statement condition ) statement
> > >
> > > The paper talks about using it for switch too but says that it is not
> > > proposing then. Let's say we accept that the benefit would be small.
> > >
> > > Regular while and for loops already have init statements.
> >
> > Since when do while loops have init statements? Or are you just talking
> > about `for` loops that don't have an increment statement?
>
> I was thinking of how you can declare variables in them, but I didn't think
> it through. It's the same syntax as plain old if, which means that anything
> that applies to if should apply to while too.

Thinking it a little more through, we don't need it for while, as it can be
converted to a for:

for ( init; loop_condition; )

Zhihao Yuan

unread,
Jun 25, 2016, 1:11:36 AM6/25/16
to std-pr...@isocpp.org
On Fri, Jun 24, 2016 at 11:09 PM, Thiago Macieira <thi...@macieira.org> wrote:
>
> for (auto list = get_list(); auto entry : list) {
> use_both(entry, list);
> }
>

Looks hard to read :(

if (auto list = get_list()) {
for (auto entry : list)
use_both(entry, list);
}

Looks clearer to me.

> Provided this new extension is worded properly, it would also solve the
> problem of the dangling temporaries that the current ranged for calls for.

I think we should just solve it.

--
Zhihao Yuan, ID lichray
The best way to predict the future is to invent it.
___________________________________________________
4BSD -- http://blog.miator.net/

Zhihao Yuan

unread,
Jun 25, 2016, 1:25:27 AM6/25/16
to std-pr...@isocpp.org
On Sat, Jun 25, 2016 at 12:11 AM, Zhihao Yuan <z...@miator.net> wrote:
>
> if (auto list = get_list()) {
> for (auto entry : list)
> use_both(entry, list);
> }

Oh this is _init-statement_ not _condition_, then

#define with(init) if(init; true)

with (auto list = get_list()) {
for (auto entry : list)
use_both(entry, list);
}

Reply all
Reply to author
Forward
0 new messages