Can the 'auto' keyword be used as a storage class specifier in C++11?
Asked Answered
S

2

10

Can the auto keyword be used as a storage class specifier in C++11?

Is the following code legal in C++11?

int main() {
   auto int x;
}
Suppletion answered 22/5, 2011 at 11:22 Comment(0)
D
13

No the code is ill-formed in C++11. auto in C++11 would be used to deduce the type of a variable from its initializer and it can't be used as a storage class specifier.

Correct Usage

int main()
{
   auto x = 12; // x is an int
   auto y = 12.3; // y is a double
}
Deathless answered 22/5, 2011 at 11:37 Comment(4)
So now it is C++ 11, is it? I am getting confused. I hope this gets resolved by some official statement soon :)Tomika
@Space_C0wb0y : Yes most probably the official name would be C++11 :)Deathless
@Space_C0wb0y: not quite yet, when Sutter was asked the question he said he'd rather wait (no need to rush now) that the standard was really agreed upon before dubbing C++0x with its definitive millesime.Sphalerite
@Björn Pollex: Only if ISO makes it before January. If not, then not. Therefore, C++0x is still the more definite term (imho).Digital
E
0
auto int x;

is circular - you are literally declaring the type as an int. given that you had this information - there is no reason to not simply use:

int x;

if you wanted to declare x the type of another variable in scope you can use decltype

using sometype = float;
sometype y;
decltype(y) x;
Empyema answered 3/10, 2016 at 23:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.