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

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

问题描述

可能的重复:
C++0x auto 关键字太多了

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

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 的地方是当您使用 new1make_* 函数时,例如如这里:

Another place where auto can be used is when you use new1 or make_* functions , such as here:

//without auto. Not that good, looks cumbersome
SomeType<OtherType>::SomeOtherType * obj1 = new SomeType<OtherType>::SomeOtherType();
std::shared_ptr<XyzType> obj2 = std::make_shared<XyzType>(args...);
std::unique_ptr<XyzType> obj2 = std::make_unique<XyzType>(args...);

//With auto. good : auto increases readability here
auto obj1 = new SomeType<OtherType>::SomeOtherType();
auto obj2 = std::make_shared<XyzType>(args...);
auto obj3 = std::make_unique<XyzType>(args...);

这里非常好,因为它减少了键盘的使用,但不降低可读性,因为任何人都可以通过查看代码知道正在创建的对象的类型.

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

1.避免使用 new 和原始指针.

1. Avoid using new and raw-pointers though.

有时,类型是如此无关紧要以至于甚至不需要类型的知识,例如在表达式模板中;实际上,实际上不可能(正确地)编写类型,在这种情况下,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 应该使用,如果上下文没有说清楚并且不是很常见(例如上面的第二种情况),那么它最好避免.

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.

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

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