为什么需要显式编写'auto'关键字? [英] Why do I need to explicitly write the 'auto' keyword?
本文介绍了为什么需要显式编写'auto'关键字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在从C ++ 98迈向C ++ 11,并且已经熟悉auto
关键字.我想知道为什么如果编译器能够自动推断类型,我们为什么需要显式声明auto
.我知道C ++是一种强类型的语言,这是一条规则,但是如果不显式声明变量auto
,是否不可能实现相同的结果?
I am moving towards C++11 from C++98 and have become familiar with the auto
keyword. I was wondering why we need to explicitly declare auto
if the compiler is able to automatically deduce the type. I know C++ is a strongly typed language and this is a rule but was it not possible to achieve the same outcome without explicitly declaring a variable auto
?
推荐答案
删除显式auto
会破坏语言:
例如
int main()
{
int n;
{
auto n = 0; // this shadows the outer n.
}
}
在其中您可以看到,放下auto
不会遮盖外在n
.
where you can see that dropping the auto
would not shadow the outer n
.
这篇关于为什么需要显式编写'auto'关键字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文