auto是一个可选的基于范围的for循环的关键字? [英] Is auto an optional keyword in ranged based for loops?

查看:442
本文介绍了auto是一个可选的基于范围的for循环的关键字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我记得有人曾经告诉我,


不需要 auto 在基于范围的for循环中,
not 在语言中是不明确的,如果我们删除它的话。

这是一个真正的语句吗?

以下代码是有效的C ++语法吗?

  for(elem:range){...} 

有效的语法,但是当我去编译与

铿锵++ - std = c ++ 1z ,我被显示以下错误:

 基于范围的循环需要输入循环变量
(elem:range){
解决方案



  for(elem:range){...} 

语法当前不是有效的,建议使这个有效的语法和语法在gcc 5.2中得到支持( 实时查看 ):

  #include< vector> 

int main()
{
std :: vector< int> v; (elem:v)
{
}
}

$ b $

b

,如果我们在C ++ 14模式下试试这个,它会说:

lockquote
警告:基于范围的没有类型说明符的循环只有
和-std = c ++ 1z或-std = gnu ++ 1z


所以这显然会起作用,并已在gcc中实施。它看起来像这个功能已被删除在gcc 6.0中。

据我可以告诉这是在gcc中实现与期望提案N3853:基于范围的For-Loops:下一代将被接受但被拒绝了,更新后的版本 N3994 < a>表示:
$ b


这会更新N3853(参见[1]),它提出了for(elem:
range) ,增加对属性的支持并回答额外的
问题。请参阅原始提案,了解
背后的基本原理,这里不再赘述。


我们可以看到它是拒绝从 EWG第81期我们也可以从厄巴纳会议分钟。虽然提案中有很多问题,但我相信STL在提案的问答部分提出了一系列令人信服的论点,我对提案被拒绝表示失望。


I recall someone once telling me,

"there is no need for auto inside range-based for loops. It would not be ambiguous in the language if we were to remove it."

Is that a true statement?
Is the folowing code valid C++ syntax?

for (elem : range){...}  

I had assumed this was already valid syntax, but when I went to compile with
clang++ --std=c++1z, I was shown the following error:

range-based for loop requires type for loop variable
 for (elem: range){

The compiler still recognizes this as a range-based for loop, so why can't it also derive the type?

解决方案

The:

for (elem : range){...}  

syntax is not currently valid but there was a proposal to make this valid syntax and the syntax is supported in gcc 5.2 (see it live):

#include <vector>

int main()
{
    std::vector<int> v ;

    for( elem : v )
    {
    }
}

and if we try this in C++14 mode it says:

warning: range-based for loop without a type-specifier only available with -std=c++1z or -std=gnu++1z

So this would clearly work and has been implemented in gcc. It looks like this feature was removed in gcc 6.0.

As far as I can tell this was implemented in gcc with the expectation that proposal N3853: Range-Based For-Loops: The Next Generation would be accepted but it was rejected and the updated version N3994 says:

This updates N3853 (see [1]) which proposed the syntax "for (elem : range)", by adding support for attributes and answering additional questions. Please see the original proposal for the rationale behind this feature, which is not repeated here.

We can see it was rejected from the EWG issue 81 and we can also see this from the Urbana meeting minutes. Although there are many issues with the proposal I believe STL made a convincing set of arguments in the Question and Answers section of the proposal and I was disappointed that the proposal was rejected.

这篇关于auto是一个可选的基于范围的for循环的关键字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆