Topic: Lack of decltype in range-based for specification


Author: Scott Meyers <usenet@aristeia.com>
Date: Sat, 6 Jun 2009 09:58:24 CST
Raw View
6.5.4/1 shows how

    for ( for-range-declaration : expression ) statement

is transformed into an equivalent code block containing this code:

    for ( auto __begin = std::Range<_RangeT>::begin(__range),
               __end = std::Range<_RangeT>::end(__range);

where "_RangeT is the type of the expression."  Given the existence of decltype,
this seems unnecessarily vague to me.  Is there a reason not revise the code to
read as follows?

    for ( auto __begin = std::Range<decltype(expression)>::begin(__range),
               __end = std::Range<decltype)expression>::end(__range);

Thanks for any clarification you can offer.

Scott

--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: wasti.redl@gmx.net
Date: Sun, 7 Jun 2009 20:14:45 CST
Raw View
On Jun 6, 5:58 pm, Scott Meyers <use...@aristeia.com> wrote:
> 6.5.4/1 shows how
>
>     for ( for-range-declaration : expression ) statement
>
> is transformed into an equivalent code block containing this code:
>
>     for ( auto __begin = std::Range<_RangeT>::begin(__range),
>                __end = std::Range<_RangeT>::end(__range);
>
> where "_RangeT is the type of the expression."  Given the existence of decltype,
> this seems unnecessarily vague to me.  Is there a reason not revise the code to
> read as follows?
>
>     for ( auto __begin = std::Range<decltype(expression)>::begin(__range),
>                __end = std::Range<decltype)expression>::end(__range);
>
> Thanks for any clarification you can offer.

The type of the expression is never a reference. decltype(expression)
could be a reference. That would be wrong.

Sebastian


--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: litb <Schaub-Johannes@web.de>
Date: Sun, 7 Jun 2009 21:51:11 CST
Raw View
On 6 Jun., 17:58, Scott Meyers <use...@aristeia.com> wrote:
> 6.5.4/1 shows how
>
>     for ( for-range-declaration : expression ) statement
>
> is transformed into an equivalent code block containing this code:
>
>     for ( auto __begin = std::Range<_RangeT>::begin(__range),
>                __end = std::Range<_RangeT>::end(__range);
>
> where "_RangeT is the type of the expression."  Given the existence of decltype,
> this seems unnecessarily vague to me.  Is there a reason not revise the code to
> read as follows?
>
>     for ( auto __begin = std::Range<decltype(expression)>::begin(__range),
>                __end = std::Range<decltype)expression>::end(__range);
>
> Thanks for any clarification you can offer.
>

I think it could have something to do with the reference thing
decltype adds. So if you have a lvalue expression that isn't an id-
expression, it will give you T& . But the type of the expression is
still "lvalue T". You need T[N] for an array, instead of T(&)[N], for
example. remove_reference then will complicate that short rule
"_RangeT is the type of the expression":

   std::Range<remove_reference<decltype(expression)>::type>::begin
(__range)

And it would need to do a special case for unconstrained templates,
adding typename. I think this would complicate the matter more than it
would reduce it.


--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: Mathias Gaunard <loufoque@gmail.com>
Date: Sun, 7 Jun 2009 21:51:48 CST
Raw View
On 6 juin, 17:58, Scott Meyers <use...@aristeia.com> wrote:
> 6.5.4/1 shows how
>
>     for ( for-range-declaration : expression ) statement
>
> is transformed into an equivalent code block containing this code:
>
>     for ( auto __begin = std::Range<_RangeT>::begin(__range),
>                __end = std::Range<_RangeT>::end(__range);
>
> where "_RangeT is the type of the expression."  Given the existence of decltype,
> this seems unnecessarily vague to me.  Is there a reason not revise the code to
> read as follows?
>
>     for ( auto __begin = std::Range<decltype(expression)>::begin(__range),
>                __end = std::Range<decltype)expression>::end(__range);

Or even simpler,

auto __begin = std::begin(__range), __end = std::end(range);

Which works like a charm in C++03 (Boost.Range does it).


--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]