是否有任何理由要在C ++ 03的'自动'的关键字? [英] Is there any reason to use the 'auto' keyword in C++03?

查看:148
本文介绍了是否有任何理由要在C ++ 03的'自动'的关键字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意这个问题最初发布于2009年,之前C ++ 11被批准和汽车的意义之前关键字被彻底改变。提供的答案涉及的只有汽车的C ++ 03的意义 - 这是一个存储类指定的 - 而不是C ++ 汽车的11含义 - 这是自动的类型推导。如果您正在寻找有关何时使用C ++ 11 汽车的意见,这个问题是不相关的这个问题。

Note this question was originally posted in 2009, before C++11 was ratified and before the meaning of the auto keyword was drastically changed. The answers provided pertain only to the C++03 meaning of auto -- that being a storage class specified -- and not the C++11 meaning of auto -- that being automatic type deduction. If you are looking for advice about when to use the C++11 auto, this question is not relevant to that question.

,因为我认为没有理由使用静态关键字C中的时间最长,因为块范围内声明的变量是隐含全球。后来我发现,变量声明为静态块范围内会给它永久持续时间,以及块范围之外声明它(在程序范围内)将给予文件-scope(只能在编译单元访问)。

For the longest time I thought there was no reason to use the static keyword in C, because variables declared outside of block-scope were implicitly global. Then I discovered that declaring a variable as static within block-scope would give it permanent duration, and declaring it outside of block-scope (in program-scope) would give it file-scope (can only be accessed in that compilation unit).

因此​​,这给我留下了只有一个关键词,我(也许)还没有完全明白:汽车关键字。有一些其他的意义比其他的局部变量?任何它未暗中为你做任何你可能想使用它?如何在程序范围内的汽车变量的行为?一个在文件范围静态自动变量是什么?这是否关键字比其他任何目的的只是现有的完整性的?

So this leaves me with only one keyword that I (maybe) don't yet fully understand: The auto keyword. Is there some other meaning to it other than 'local variable?' Anything it does that isn't implicitly done for you wherever you may want to use it? How does an auto variable behave in program scope? What of a static auto variable in file-scope? Does this keyword have any purpose other than just existing for completeness?

推荐答案

汽车是一个存储类说明,静态注册的extern 太。您只能使用这四个中的一个声明。

auto is a storage class specifier, static, register and extern too. You can only use one of these four in a declaration.

局部变量(不包括静态)具有自动存储时间,这意味着他们从定义开始生活,直到他们的块结束。在他们面前把汽车是多余的,因为这是默认反正。

Local variables (without static) have automatic storage duration, which means they live from the start of their definition until the end of their block. Putting auto in front of them is redundant since that is the default anyway.

我不知道什么原因在C ++中使用它。在具有隐含INT规则旧的C版本,你可以用它来声明一个变量

I don't know of any reason to use it in C++. In old C versions that have the implicit int rule, you could use it to declare a variable

int main(void) { auto i = 1; }

要让它有效的语法或转让前pression歧义的情况下, I 在范围内。但是,这并不在C ++中的工作呢(你必须指定一个类型)。够好笑的是,C ++标准写道:

To to make it valid syntax or disambiguate from an assignment expression in case i is in scope. But this doesn't work in C++ anyway (you have to specify a type). Funny enough, the C++ Standard writes:

这是对象不声明在块范围存储类说明符或声明为函数参数在默认情况下自动存储时间。 [注:因此,汽车符几乎总是多余的,不经常使用;使用一次自动的是从一个前pression语句(6.8)明确区分的声明语句。末端注意事项】

An object declared without a storage-class-specifier at block scope or declared as a function parameter has automatic storage duration by default. [Note: hence, the auto specifier is almost always redundant and not often used; one use of auto is to distinguish a declaration-statement from an expression-statement (6.8) explicitly. —end note]

这是指以下情形,这可以是 A 为int或一个变量的声明 A INT 周围有 A 多余的括号。它总是采取的是一个宣言,让汽车就不会在这里添加任何东西很有用,但会为人类,而不是。但话又说回来,人会变得更好去除周围 A 冗余括号,我会说。

Which refers to the following scenario, which could be either a cast of a to int or the declaration of a variable a of type int having redundant parentheses around a. It is always taken to be a declaration, so auto wouldn't add anything useful here, but would for the human, instead. But then again, the human would be better off removing the redundant parentheses around a, I would say.

int(a);

使用汽车用C ++ 0x中到达的新的含义,我会劝阻使用它与C ++ 03在code的含义。

With the new meaning of auto arriving with C++0x, I would discourage using it with C++03's meaning in code.

这篇关于是否有任何理由要在C ++ 03的'自动'的关键字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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