Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 in C++11?

Is the following code legal in C++11?

int main() {
   auto int x;
}
like image 787
Sergii Avatar asked May 22 '11 11:05

Sergii


People also ask

What is auto keyword in C++11?

The auto keyword directs the compiler to use the initialization expression of a declared variable, or lambda expression parameter, to deduce its type.

What is the use of auto keyword in C?

auto: This is the default storage class for all the variables declared inside a function or a block. Hence, the keyword auto is rarely used while writing programs in C language. Auto variables can be only accessed within the block/function they have been declared and not outside them (which defines their scope).

Which keyword is used in automatic storage class?

The initialization of these variables is, by default, a garbage value. The memory that is assigned to an automatic variable gets free when it exits from a block. We use the keyword auto to define the automatic variables.

Which is not a storage class specifier?

Which of the following is not a storage class specifier in C? Explanation: volatile is not a storage class specifier. volatile and const are type qualifiers.


1 Answers

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
}
like image 157
Prasoon Saurav Avatar answered Oct 28 '22 10:10

Prasoon Saurav