Is C++11 auto type dangerous? [duplicate]

烈酒焚心 提交于 2019-12-07 23:47:55

问题


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?


回答1:


My personal experience is auto is handy for generic code, or things like range-based for loop, but you might get something like

auto count = getCount();
if (count < 0) {
  // do something
}

If getCount() returns an unsigned number, instead of what you might be expecting (int), you won't even get a warning.



来源:https://stackoverflow.com/questions/8430053/is-c11-auto-type-dangerous

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!