是否有任何理由使用“自动”? C++03 中的关键字?
注意这个问题最初发布于 2009 年,当时 C++11 尚未获得批准,
auto
关键字的含义还没有发生彻底改变。 提供的答案仅涉及auto
的 C++03 含义(即指定的存储类),而不是的 C++11 含义auto--自动类型推导。 如果您正在寻找有关何时使用 C++11
auto
的建议,此问题与该问题无关。
在很长一段时间里,我认为没有理由在 C 中使用 static 关键字,因为在块作用域之外声明的变量是隐式全局的。 然后我发现在块作用域内将变量声明为 static 会赋予它永久的持续时间,而在块作用域之外(在程序作用域中)声明它会赋予它文件作用域(只能是在该编译单元中访问)。
因此,这只剩下一个我(可能)尚未完全理解的关键字:auto
关键字。 除了“局部变量”之外,它还有其他含义吗? 无论您想在何处使用它,它所做的任何事情都不是隐式为您完成的吗? auto
变量在程序范围内的行为如何? 文件范围内的static auto
变量怎么样? 除了为了完整性而存在之外,该关键字还有其他目的吗?
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 ofauto
-- that being a storage class specified -- and not the C++11 meaning ofauto
-- that being automatic type deduction. If you are looking for advice about when to use the C++11auto
, this question is not relevant to that question.
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
除了“局部变量”之外,“auto”还有其他含义吗?
C++03 中没有。
它所做的任何事情都不是在您想要使用它的任何地方隐式完成的?
在 C++03 中什么都没有。
自动变量在程序范围内的行为如何? 文件范围内的静态自动变量怎么样?
函数/方法体之外不允许使用关键字。
这个关键字除了为了完整性而存在之外,[在 C++03 中]还有其他目的吗?
令人惊讶的是,是的。 C++ 设计标准包括与 C 的高度向后兼容性。C 有这个关键字,没有真正的理由禁止它或在 C++ 中重新定义它的含义。 所以,目的是减少与 C 的不兼容性。
除了为了完整性而存在之外,这个关键字在 C 中还有其他用途吗?
我最近才了解到一个:易于移植来自 B.C 的古代程序。来自一种名为 B 的语言,其语法与 C 的语法非常相似。但是,B 没有任何类型。 在 B 中声明变量的唯一方法是指定其存储类型(
auto
或extern
)。 像这样:此语法在 C 中仍然有效,相当于
因为在C中,存储类默认为
auto
,类型默认为int
。 我猜想当时每个源自 B 并移植到 C 的程序实际上都充满了auto
变量。C++03 不再允许 C 风格的隐式 int,但它保留了不再完全有用的
auto
关键字,因为与隐式 int 不同,它不会在C 的语法Is there some other meaning to 'auto' other than 'local variable?'
Not in C++03.
Anything it does that isn't implicitly done for you wherever you may want to use it?
Nothing whatsoever, in C++03.
How does an auto variable behave in program scope? What of a static auto variable in file-scope?
Keyword not allowed outside of a function/method body.
Does this keyword have any purpose [in C++03] other than just existing for completeness?
Surprisingly, yes. C++ design criteria included a high degree of backward compatibility with C. C had this keyword and there was no real reason to ban it or redefine its meaning in C++. So, the purpose was one less incompatibility with C.
Does this keyword have any purpose in C other than just existing for completeness?
I learned one only recently: ease of porting of ancient programs from B. C evolved from a language called B whose syntax was quite similar to that of C. However, B had no types whatsoever. The only way to declare a variable in B was to specify its storage type (
auto
orextern
). Like this:This syntax still works in C and is equivalent to
because in C, the storage class defaults to
auto
, and the type defaults toint
. I guess that every single program that originated in B and was ported to C was literally full ofauto
variables at that time.C++03 no longer allows the C style implicit int, but it preserved the no-longer-exactly-useful
auto
keyword because unlike the implicit int, it wasn't known to cause any trouble in the syntax of C.在旧的编译器中,auto 是声明局部变量的一种方法。 如果没有 auto 关键字或类似关键字,则无法在 Turbo C 等旧编译器中声明局部变量。
In old compiler, auto was one way to declare a local variable at all. You can't declare local variables in old compilers like Turbo C without the auto keyword or some such.
在 C++11 中,
auto
具有新的含义:它允许您自动推断变量的类型。为什么它有用? 让我们考虑一个基本示例:
其中的
auto
创建一个std::list::iterator
类型的迭代器。这可以使一些非常复杂的代码更容易阅读。
另一个例子:
其中,
auto
推导了在变量中存储 lambda 表达式所需的类型。维基百科对该主题有很好的报道。
In C++11,
auto
has new meaning: it allows you to automatically deduce the type of a variable.Why is that ever useful? Let's consider a basic example:
The
auto
there creates an iterator of typestd::list<int>::iterator
.This can make some seriously complex code much easier to read.
Another example:
There, the
auto
deduced the type required to store a lambda expression in a variable.Wikipedia has good coverage on the subject.
auto
是一个存储类说明符,static
、register
和extern
也是。 您只能在声明中使用这四种方法之一。局部变量(没有
static
)具有自动存储持续时间,这意味着它们从定义开始一直到块结束。 将 auto 放在它们前面是多余的,因为无论如何这是默认的。我不知道有什么理由在 C++ 中使用它。 在具有隐式 int 规则的旧 C 版本中,您可以使用它来声明变量,例如:
为了使其语法有效或在
i
位于范围内的情况下消除赋值表达式的歧义。 但这在 C++ 中无论如何都行不通(你必须指定一个类型)。 有趣的是,C++ 标准写道:指的是以下场景,可以是
a
到int
的强制转换,也可以是变量a
的声明类型int
在a
周围有多余的括号。 它始终被视为一个声明,因此auto
不会在这里添加任何有用的东西,但会为人类添加。 但话又说回来,人类最好删除a
周围的多余括号,我会说:随着 C++0x 带来的
auto
的新含义,我会不鼓励在代码中将其与 C++03 的含义一起使用。auto
is a storage class specifier,static
,register
andextern
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.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, like in:
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:which refers to the following scenario, which could be either a cast of
a
toint
or the declaration of a variablea
of typeint
having redundant parentheses arounda
. It is always taken to be a declaration, soauto
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 arounda
, I would say:With the new meaning of
auto
arriving with C++0x, I would discourage using it with C++03's meaning in code.auto 关键字目前没有任何用途。 你是完全正确的,它只是重申了局部变量的默认存储类,真正有用的替代方案是
static
。它在 C++0x 中具有全新含义。 这让你知道它是多么无用!
The auto keyword has no purpose at the moment. You're exactly right that it just restates the default storage class of a local variable, the really useful alternative being
static
.It has a brand new meaning in C++0x. That gives you some idea of just how useless it was!
GCC 对嵌套函数有特殊的使用
auto
- 请参阅 这里。如果您想要在定义之前调用嵌套函数,则需要使用
auto
声明它。GCC has a special use of
auto
for nested functions - see here.If you have nested function that you want to call before its definition, you need to declare it with
auto
.“auto”据说告诉编译器自己决定将变量放在哪里(内存或寄存器)。 它的模拟是“寄存器”,据说它告诉编译器尝试将其保存在寄存器中。 现代编译器会忽略这两者,因此您也应该这样做。
"auto" supposedly tells the compiler to decide for itself where to put the variable (memory or register). Its analog is "register", which supposedly tells the compiler to try to keep it in a register. Modern compilers ignore both, so you should too.
对于基于堆栈的处理器,我使用此关键字来显式记录函数何时将变量放置在堆栈上。 在从函数(或中断服务例程)返回之前修改堆栈时,可能需要此函数。
在本例中,我声明:
然后我访问变量外部:
因此
auto
关键字有助于记录意图。I use this keyword to explicitly document when it is critical for function, that the variable be placed on the stack, for stack-based processors. This function can be required when modifying the stack prior to returning from a function (or interrupt service routine).
In this case I declare:
And then I access outside the variable:
So the
auto
keyword helps document the intent.根据 Stroustrup 的说法,在《C 编程语言》(第 4 版,涵盖 C 11)中,使用 'auto' 有以下几个主要原因(第 2.2.2 节)(引用了 Stroustrup 的话):
1)
通过“auto”及其必要的初始化器,我们可以一目了然地知道变量的类型!
2)
在我看来,适合这里的情况是这样的:
3)
想象一下模板化迭代器中的一些长类型名称:(
第 6.3.6.1 节中的代码)
According to Stroustrup, in "The C Programming Language" (4th Edition, covering C 11), the use of 'auto' has the following major reasons (section 2.2.2) (Stroustrup words are quoted):
1)
With 'auto' and its necessary initializer we can know the variable's type in a glance!
2)
In my opinion a case that fits here, is something like this:
3)
Imagine some long type name from a templatized iterator:
(code from section 6.3.6.1)
Microsoft 的 Stephan T. Lavavej 在 MSDN 的 Channel 9 站点上免费观看/下载的 STL 视频讲座中很好地描述了 C++0x 中 auto 关键字的新含义 此处。
整个讲座值得一看,但有关 auto 关键字的部分大约在第 29 分钟(大约)。
The new meaning of the auto keyword in C++0x is described very nicely by Microsoft's Stephan T. Lavavej in a freely viewable/downloadable video lecture on STL found at MSDN's Channel 9 site here.
The lecture is worth viewing in its entirety, but the part about the auto keyword is at about the 29th minute mark (approximately).