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

旧城冷巷雨未停 提交于 2019-12-03 02:23:52

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.

Kyle Strand

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


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.

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

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