为什么C ++在一个自动语句中不允许多种类型? [英] Why does C++ not allow multiple types in one auto statement?

查看:69
本文介绍了为什么C ++在一个自动语句中不允许多种类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

2011 C ++标准引入了新的关键字auto,该关键字可用于定义变量而不是类型,即

The 2011 C++ standard introduced the new keyword auto, which can be used for defining variables instead of a type, i.e.

auto p=make_pair(1,2.5);                   // pair<int,double>
auto i=std::begin(c), end=std::end(c);     // decltype(std::begin(c))

在第二行中,iend具有相同的类型,称为auto.该标准不允许

In the second line, i and end are of the same type, referred to as auto. The standard does not allow

auto i=std::begin(container), e=std::end(container), x=*i;

,当x是不同类型时. 我的问题:为什么标准不允许最后一行?可以通过将auto解释为不代表某种待判定的类型,而表示将声明为auto any 变量的类型推导为准. C ++ 11标准是否有充分的理由不采用这种方法?

when x would be of different type. My question: why does the standard not allow this last line? It could be allowed by interpreting auto not as representing some to-be-decuded type, but as indicating that the type of any variable declared auto shall be deduced from its assigned value. Is there any good reason for the C++11 standard to not follow this approach?

实际上有一个用例,即在for循环的初始化语句中:

There is actually a use case for this, namely in the initialisation statement of for loops:

for(auto i=std::begin(c), end=std::end(c), x=*i;  i!=end;  ++i, x+=*i)
{ ... }

当变量iendx的范围仅限于for循环时. AFAIK,除非这些变量具有通用类型,否则在C ++中无法实现. 这是正确的吗?(将所有类型放入struct中的丑陋技巧)

when the scope of the variables i, end, and x is limited to the for loop. AFAIK, this cannot be achieved in C++ unless those variables have a common type. Is this correct? (ugly tricks of putting all types inside a struct excluded)

某些可变参数模板应用程序中可能还会有一些用例.

There may also be use cases in some variadic template applications.

推荐答案

我认为与非auto声明的一致性只是一个问题.

I think it's just a matter of consistency with non-auto declarations.

此:

auto n = 42, *p = &n;

等效于:

int n = 42, *p = &n;

其中,类型intint*从初始化程序派生.在这两种情况下,即使intint*是不同的类型,由于它们紧密的语法关系,也允许它们在同一声明中. (通过遵循C和C ++声明几乎的声明遵循使用"规则,您可以将n*p都定义为int类型.)

where the types int and int* are derived from the initializers. In both cases, even though int and int* are different types, they're permitted to be in the same declaration because of their close syntactic relation. (By the "declaration follows use" rule that C and C++ declarations almost follow, you're defining both n and *p as being of type int.)

可能会在同一声明中允许不相关的类型:

It would have been possible to permit unrelated types in the same declaration:

auto n = 42, x = 1.5;

,但以上内容必须等同于两个单独的声明:

but the above would have to be equivalent to two separate declarations:

int n = 42; double x = 1.5;

我认为添加auto的想法是对语言进行最小的更改,允许从初始化程序中推断类型,但不更改可能的声明类型.

I think the idea when adding auto was to make a minimal change to the language, permitting the type to be inferred from an initializer but not changing the kinds of declarations that are possible.

即使没有auto,也可以在for循环头中定义intint*:

Even without auto, you could define an int and an int* in a for loop header:

for (int n = 42, *p = &n; expr1; expr2) { /* ... / }

,但是不能一起声明intdouble. auto的添加并没有改变.

but you couldn't declare an int and a double together. The addition of auto simply didn't change that.

for循环的上下文之外,通常还是最好使用单独的声明.在大多数情况下,将许多不同的声明放入for循环中可能是一个坏主意.对于需要大量声明的(可能很少)情况,您可以将它们放在循环上方,如下所示:

Outside the context of a for loop, it's generally much better to use separate declarations anyway. Shoving a lot of different declarations into a for loop is arguably a bad idea in most cases. And for the (probably rare) cases where you want a lot of declarations, you can just put them above the loop, something like this:

auto i=std::begin(c), end=std::end(c),
for( x=*i;  i!=end;  ++i, x+=*i) {
    // ...
}

如果要限制范围,请在整个内容周围添加另一组{ }. (在这种情况下,您可能还是希望end成为const.)

adding another set of { } around the whole thing if you want to limit the scope. (In this case, you'd probably want end to be const anyway.)

这篇关于为什么C ++在一个自动语句中不允许多种类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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