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

查看:73
本文介绍了在 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?

推荐答案

您只询问了缺点,所以我要强调其中的一些.如果使用得当,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 是什么类型.它可能是一个 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() 存在哪些重载,结果类型可能完全不同 - 因此后续代码的行为可能与预期完全不同.您可能会在后面的代码中突然触发错误消息(例如,随后尝试取消引用 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天全站免登陆