auto Keyword

Started by
4 comments, last by Zahlman 18 years, 3 months ago
I have some questions about the auto keyword. 1) I have seen declarations like this:
auto int i = 0;
What does the auto do? 2) I have also seen this:
v is a vector

for (auto i = v.begin(); i != v.end(); ++v)
     // do something here...
where the auto keyword detects that v is a vector and initializes i accordingly (or something like that though I think that might be C++0x). Can you do this with C++98? 3) If, like in the last example, auto automatically detects the type of v, could I do something like this:
int integer = 23145496;
auto automajigger = integer;
Thanks in advance.
F-R-E-D F-R-E-D-B-U-R...G-E-R! - Yes!
Advertisement
1) It means it's a stack variable. It's basically pointless.

2) That's C++0x code, you can't do it with C++98.

3) If you had a C++0x compiler.
Okay, that clears things up a bit.

Thanks!
F-R-E-D F-R-E-D-B-U-R...G-E-R! - Yes!
Would 'auto' then be the complement keyword to 'register'?
register and auto are both storage-class specifiers (as are static and extern) so I would consider them to be complementary. Not necessarily opposites, but complementary.
They are not "opposites"; there is a third option for local storage - static, and you could always put stuff on the freestore instead.

This topic is closed to new replies.

Advertisement