在C ++中用auto声明变量有缺点吗? [英] Is there a downside to declaring variables with auto in C++?

查看:605
本文介绍了在C ++中用auto声明变量有缺点吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看来, auto 是一个相当重要的功能,在C ++ 11中添加,似乎遵循很多新的语言。像Python一样的语言,我没有看到任何明确的变量声明(我不知道是否可能使用Python标准)。

It seems that auto was a fairly significant feature to be added in C++11 that seems to follow a lot of the newer languages. As with a language like Python, I have not seen any explicit variable declaration (I am not sure if it is possible using Python standards).

使用 auto 来声明变量,而不是显式声明它们?

Is there a drawback to using auto to declare variables instead of explicitly declaring them?

推荐答案

ve只是问关于缺点,所以我突出其中一些。当使用得很好时, auto 也有几个优点。缺点是易于滥用,并增加了潜在的代码以无意的方式表现。

You've only asked about drawbacks, so I'm highlighting some of those. When used well, auto has several advantages as well. The drawbacks result from ease of abuse, and from increased potential for code to behave in unintended ways.

主要的缺点是,通过使用 auto ,您不一定知道正在创建的对象的类型。还有一些情况下,程序员可能希望编译器推导出一种类型,但是编译器坚决推断另一种类型。

The main drawback is that, by using auto, you don't necessarily know the type of object being created. There are also occasions where the programmer might expect the compiler to deduce one type, but the compiler adamantly deduces another.

给定一个声明如

auto result = CallSomeFunction(x,y,z);

你不一定知道什么类型 result is。它可能是 int 。它可能是一个指针。它可能是别的东西。所有这些都支持不同的操作。你还可以通过一个小的更改,例如

you don't necessarily have knowledge of what type result is. It might be an int. It might be a pointer. It might be something else. All of those support different operations. You can also dramatically change the code by a minor change like

auto result = CallSomeFunction(a,y,z);

,因为 CallSomeFunction $ c>结果的类型可能完全不同 - 后续代码因此可能与预期完全不同。您可能会在后面的代码中突然触发错误消息(例如,随后尝试取消引用 int ,尝试更改现在 const )。更危险的变化是你的变化超过编译器的地方,但后续的代码在不同的和未知的 - 可能是错误的 - 方式。

because, depending on what overloads exist for CallSomeFunction() the type of result might be completely different - and subsequent code may therefore behave completely differently than intended. You might suddenly trigger error messages in later code(e.g. subsequently trying to dereference an int, trying to change something which is now const). The more sinister change is where your change sails past the compiler, but subsequent code behaves in different and unknown - possibly buggy - ways.

没有明确知道类型一些变量因此使得更难以严格地证明该代码按预期工作的说法。这意味着在高关键性(例如安全关键或任务关键)领域,更有力地证明适合目标的说法。

Not having explicit knowledge of the type of some variables therefore makes it harder to rigorously justify a claim that the code works as intended. This means more effort to justify claims of "fit for purpose" in high-criticality (e.g. safety-critical or mission-critical) domains.

另一个更常见的缺点,是一个程序员使用 auto 作为一个钝器械来强制代码编译,而不是考虑代码正在做什么,并努力做到正确的诱惑。

The other, more common drawback, is the temptation for a programmer to use auto as a blunt instrument to force code to compile, rather than thinking about what the code is doing, and working to get it right.

这篇关于在C ++中用auto声明变量有缺点吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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