开发者

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

Can the auto keyword be used as a storage class specifier i开发者_如何学Pythonn C++11?

Is the following code legal in C++11?

int main() {
   auto int x;
}


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
}


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;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜