Which IDEs and text editors can deduce type of variables declared using auto keyword in C++11

孤者浪人 提交于 2019-12-20 13:00:09

问题


In "Almost always auto" article Herb Sutter lists several reasons for declaring variables using auto keyword.

He says that actual variable type can be automatically deduced by IDE and shown by hovering over variable name.

I would like to know which IDEs and text editors (or plugins) currently support "auto" variable type deduction.

Edit:

List of IDEs from answers:

  • Visual Studio 201x
  • Eclipse
  • Qt Creator 2.7.0
  • KDevelop 4.5.1

Text editors

What about Vim, Emacs, Sublime Text, etc. - are there plugins which supports type deduction?


回答1:


Visual Studio 2010, Visual Studio 2012, and Visual Studio 2013 support type deduction for variables declared with the auto keyword. This applies both to the IntelliSense tooltips as well as auto-complete suggestions.

Starting with Visual Studio 2010 the C++ IntelliSense support was completely reworked (see Rebuilding Intellisense). IntelliSense is now driven by the Edison Design Group (EDG) C++ compiler frontend. Whatever EDG can do you will see reflected in IntelliSense.

Note that IntelliSense tooltips will display the underlying type for auto variables. It will not work up the tree again and replace portions with appropriate typedefs. On Visual Studio 2012 the following code

std::string str;

std::string::iterator i1 = str.begin();
auto i2 = str.begin();

will display the iterators as

std::basic_string<char,std::char_traits<char>,std::allocator<char> >::iterator i1

and

std::_String_iterator<std::_String_val<std::_String_base_types<char,std::allocator<char> >::_Val_types>::_Myt> i2

Given that I would happily disagree with Herb Sutter on his assessment that an IDE is enough to deduce a type when you need it. auto is great with respect to robustness, correctness and flexibility, but it surely fails to meet a developer's needs working on a large code base.




回答2:


Native support

  • Visual Studio 2010+
    • Caveat: Does not behave terribly well with typedefs; see iinspectable's answer
  • KDevelop 4.5.1+
    • Caveat: Some incorrect deductions (e.g. float literals); see Johnny's answer
  • Qt Creator 2.7.0+
  • Eclipse (not sure if via plugin or native)

Support via plugins

  • Vim plugins:
    • YCM (since merging this pull request: https://github.com/Valloric/ycmd/pull/88)
    • others? Syntastic, maybe?
  • Emacs plugins:
    • ....SURELY there is such a plugin somewhere.

Note: the first draft of this answer was created by simply combining the existing answers plus the Eclipse comment, then adding a note about Vim. Without such an "aggregate" answer, this question (and its existing answers) appears to violate the "one right answer" rule.

This should really be formatted as a table; too bad we don't have that capability here.




回答3:


KDevelop 4.5.1 also supports type deduction. Although it probably does small mistakes.

Examples:

auto i = 3;    // Deduces int
auto d = 3.0;  // Deduces double
auto f = 3.0f; // Deduces double - wrong



回答4:


Qt Creator 2.7.0 can do it too, judging from this testing source code:

class A
{
    void f();
};

int main()
{
    auto a = A();
    return 0;
}


来源:https://stackoverflow.com/questions/19625115/which-ides-and-text-editors-can-deduce-type-of-variables-declared-using-auto-key

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