6

Has anyone ever seen the storage class auto explicitly used in C/C++? If so, in what situation?

MarredCheese
  • 17,541
  • 8
  • 92
  • 91
c0m4
  • 4,343
  • 10
  • 35
  • 40
  • @Graham I dunno, I'd say that's a completely different question. Note that the feature that this particular question is asking about disappeared in C++11 so it's not surprising that there have been no "updates" in the intervening years (aside from the sole non-2008 answer that you mentioned, which incidentally is answering the wrong question) – Lightness Races in Orbit Jan 29 '19 at 00:08
  • @LightnessRacesinOrbit Fair enough. I'll retract my flag, but I still think it's useful for those stumbling upon this question, so it should probably be in 'Related:'-style comment. – Graham Jan 29 '19 at 00:10
  • Related, for those in C++11 or later: [C++ auto keyword. Why is it magic?](https://stackoverflow.com/q/7576953/8117067) – Graham Jan 29 '19 at 00:14

6 Answers6

25

auto is never useful in current C/C++ because all variables are implicitly auto. It is useful in C++0x, where it can replace the type declaration entirely - if you have a variable with an initial assignment, 'auto' will just make it the type of that assignment value, as in the comments.

alex strange
  • 1,249
  • 9
  • 9
  • Could you include an example of C++0x use? – c0m4 Oct 31 '08 at 05:35
  • 2
    auto funcptr = std::tr1::bind(&Foo::Bar, this, _1, _2); This declares "funcptr" to be of the same type as "bind" returns (which is a seriously complicated type; in old days, you'd wrap it up in a std::tr1::function template, but with auto you don't need to do that anymore). – C. K. Young Oct 31 '08 at 05:39
  • Is this a special case or does it work more in general, like auto someVar = functionThatReturnsUnknownType(); ? – c0m4 Oct 31 '08 at 05:51
  • It works in general. Basically it's allowing variable type deduction at the initialization/construction site. The compiler already does this for templates ( template void foo(T t); ) and 'auto' makes it happen in function bodies. – Dean Michael Oct 31 '08 at 07:18
4

I haven't seen auto used in code written in the last 10+ years. There is no reason to use auto since the only places you can use it is where it is implied anyway. The only reason it still exists is for backwards compatibility but it should be avoided in new code.

Robert Gamble
  • 106,424
  • 25
  • 145
  • 137
3

In GCC you might need auto to declare nested function in order to be able to define it anywhere in function body - see http://gcc.gnu.org/onlinedocs/gcc-4.1.2/gcc/Nested-Functions.html

qrdl
  • 34,062
  • 14
  • 56
  • 86
  • Right at the top of that page it says that nested functions are not supported for GNU C++. So does the auto stuff regarding nested functions apply to other implementions of C++? – c0m4 Oct 31 '08 at 19:35
  • @c0m4: It would be safest to assume that GNU C extensions like nested functions are not available with other compilers. – Jonathan Leffler Nov 01 '08 at 00:43
1

Here is an example from my code, written in C++11:

c_srgb find_in_book(const c_HVC &HVC) {
    auto b = munsell.mun_to_rgb_book.find(HVC);
    if( b != munsell.mun_to_rgb_book.end()) {
        c_srgb f = b->second;
        return f;
    } else {
        c_srgb ret;
        ret.r=ret.g=ret.b=0;
        return ret;
    }
}

I prefer that to this:

c_srgb find_in_book(const c_HVC &HVC) {
std::_Tree_iterator<std::_Tree_val<std::_Tmap_traits<dj::color::c_HVC,dj::color::c_srgb,std::less<dj::color::c_HVC>,std::allocator<std::pair<const dj::color::c_HVC,dj::color::c_srgb>>,false>>> b = munsell.mun_to_rgb_book.find(HVC);
    if( b != munsell.mun_to_rgb_book.end()) {
        c_srgb f = b->second;
        return f;
    } else {
        c_srgb ret;
        ret.r=ret.g=ret.b=0;
        return ret;
    }
}
Jive Dadson
  • 16,680
  • 9
  • 52
  • 65
1

No, it's assumed if you omit the class specifier. The only reasonable uses I can think of would be to call attention to a particular local variable that overrides, say, a global variable with the same name, or as an interview question.

Chances are, you'll confuse the poor programmer who's stuck maintaining the code!

Adam Liss
  • 47,594
  • 12
  • 108
  • 150
1

As Alex has covered, auto is used in C++0x to declare types in initialisation declarations where the type is inferred from the initialisation code.

There was a proposal for it to also be used as a return type, where the type is deduced from code returning a value. However this gave rise to an ambiguity so, at time of writing, something more consistent with C++0x's lambda syntax is being considered.

philsquared
  • 22,403
  • 12
  • 69
  • 98