新关键字“auto”;什么时候应该用它来声明一个变量类型? [英] The new keyword "auto"; When should it be used to declare a variable type?

查看:150
本文介绍了新关键字“auto”;什么时候应该用它来声明一个变量类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:




b
$ b

我们(作为一个社群)有足够的经验来确定汽车何时和/或是否被滥用?

Have we (as a community) had enough experience to determine when and/or whether auto is being abused?

我是什么真正寻找


  • 何时使用自动

  • 避免

在80%的情况下可以快速遵循的简单的经验法则。

Simple rules of thumb that can quickly be followed in 80% of cases.

作为上下文,这个问题是由我的回应引发的这里

As a context this question is sparked by my response here

推荐答案

我认为当类型在在您的项目中工作(或将工作)的程序员,则可以使用 auto ,例如以下代码:

I think when the type is very well-known amongst the co-programmers who work (or would work) in your project, then auto can be used, such as in the following code:

//good : auto increases readability here
for(auto it = v.begin(); it != v.end(); ++it) //v is some [std] container
{
      //..
}

或者,更一般来说,

//good : auto increases readability here
for(auto it = std::begin(v); it != std::end(v); ++it)//v could be array as well
{
      //..
}






不是很知名和很少使用,那么我认为 auto 似乎降低了可读性,例如:


But when the type is not very well-known and infrequently used , then I think auto seems to reduce readability, such as here:

//bad : auto decreases readability here
auto obj = ProcessData(someVariables);

在前一种情况下,使用 auto 似乎非常好,不会降低可读性,因此可以广泛使用,但在后一种情况下,会降低可读性,因此不应使用。

While in the former case, the usage of auto seems very good and doesn't reduce readability, and therefore, can be used extensively, but in the latter case, it reduces readabilty and hence shouldn't be used.

可以使用 auto 的另一个地方是使用 new ,例如此处:

Another place where auto can be used is when you use new, such as here:

//without auto. Not that good, looks cumbersome
SomeType<OtherType>::SomeOtherType pObject = new SomeType<OtherType>::SomeOtherType();

//With auto. good : auto increases readability here
auto pObject = new SomeType<OtherType>::SomeOtherType();

这是非常好的,因为它减少了键盘的使用,而不会降低可读性,因为任何人可以通过查看代码知道 pObject 的类型。

Here it is very good, as it reduces the use of keyboard, without reducing the readability, as anyone can know the type of pObject just by looking at the code.

有时,类型是如此不相关,甚至不需要类型的知识,如在表达式模板;事实上,实际上是不可能写入类型(正确),在这种情况下 auto 是程序员的救济。我写了表达式模板库,可以用作:

Sometime, the type is so irrelevant that the knowledge of the type is not even needed, such as in expression template; in fact, practically it is impossible to write the type (correctly), in such cases auto is a relief for programmers. I've written expression template library which can be used as:

foam::composition::expression<int> x;

auto s = x * x;       //square
auto c = x * x * x;   //cube
for(int i = 0; i < 5 ; i++ )
    std::cout << s(i) << ", " << c(i) << std::endl; 

输出:

0, 0
1, 1
4, 8
9, 27
16, 64

现在将上述代码与以下不使用 auto的等效代码进行比较

Now compare the above code with the following equivalent code which doesn't use auto:

foam::composition::expression<int> x;

//scroll horizontally to see the complete type!!
foam::composition::expression<foam::composition::details::binary_expression<foam::composition::expression<int>, foam::composition::expression<int>, foam::operators::multiply>> s = x * x; //square
foam::composition::expression<foam::composition::details::binary_expression<foam::composition::expression<foam::composition::details::binary_expression<foam::composition::expression<int>, foam::composition::expression<int>, foam::operators::multiply> >, foam::composition::expression<int>, foam::operators::multiply>> c = x * x * x; //cube

for(int i = 0; i < 5 ; i++ )
    std::cout << s(i) << ", " << c(i) << std::endl; 

正如你所看到的,在这种情况下 auto 使你的生活指数级更容易。上面使用的表达式很简单;考虑一些更复杂表达式的类型:

As you can see, in such cases auto makes your life exponentially easier. The expressions used above are very simple; think about the type of some more complex expressions:

auto a = x * x - 4 * x + 4; 
auto b = x * (x + 10) / ( x * x+ 12 );
auto c = (x ^ 4 + x ^ 3 + x ^ 2 + x + 100 ) / ( x ^ 2 + 10 );

这种表达式的类型会更加巨大和丑陋, > auto ,我们现在可以让编译器推断表达式的类型。

The type of such expressions would be even more huge and ugly, but thanks to auto, we now can let the compiler infer the type of the expressions.

底线是:关键字 auto 可能会根据上下文增加或减少代码的清晰度和可读性, 。如果上下文明确了什么是类型,或者至少应该如何使用(在标准容器迭代器的情况下)或者甚至不需要实际类型的知识(例如在表达式模板),那么 auto 应该被使用,如果上下文不清楚,并且不是很常见(例如第二case),那么应该更好地避免

So the bottomline is: the keyword auto might increase or decrease clarity and readability of your code, depending on the context. If the context makes it clear what type it is, or at least how it should be used (in case of standard container iterator) or the knowledge of the actual type is not even needed (such as in expression templates), then auto should be used, and if the context doesn't make it clear and isn't very common (such as the second case above), then it should better be avoided.

这篇关于新关键字“auto”;什么时候应该用它来声明一个变量类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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