C++11 汽车类型危险吗?
在C++11中,键入变量auto
而不是int
,将使编译器自动使用从其初始化上下文推导出来的正确类型。在类型很明显但写起来很无聊的情况下,这非常方便。是否有需要注意的陷阱,或者有人避免使用它的原因?
Possible Duplicate:
How much is too much with C++0x auto keyword
The new keyword “auto”; When should it be used to declare a variable type?
In C++11, Typing a variable auto
instead of, say, int
, will let the compiler automatically use the right type, deduced from its initialization context. This comes super handy in situations where the type is obvious but boring to write. Are there pitfalls to be aware of, or reasons why someone would avoid using that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我个人的经验是 auto 对于通用代码或基于范围的 for 循环之类的东西很方便,但您可能会得到类似的结果
如果 getCount() 返回一个无符号数字,而不是您可能期望的 (int),您将不会甚至收到警告。
My personal experience is auto is handy for generic code, or things like range-based for loop, but you might get something like
If getCount() returns an unsigned number, instead of what you might be expecting (int), you won't even get a warning.