Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Point of declaration for auto keyword

Tags:

c++

c++11

I had a Q&A before: Point of declaration in C++. The rule point-of-declaration nicely is applicable on many situations. Now, I confused on usage of auto in combination of this rule.

Consider these two codes:

i. Declaring x by itself (we don't expect it to work):

{
  auto x = x;
}

ii. Declaring the inner x by the outer x (It makes error in gcc 4.8.x):

{
  int x = 101; // the outer x
  {
    auto x = x; // the inner x
  }
}

According to the rule of point-of-declaration, it should work but it doesn't. It seems there is another rule in the standard that I missed it. The question is, Where is the point-of-declaration when using auto?

 

There are two possibilities:

i. If the point of declaration is after =, at the end of statement:

auto object = expression;
                        ^
                        Is it here? If it is, why gcc complains?

So the second declaration is valid and must work, because there is no x but that outer one (which is declared before). Therefore auto x=x is valid and the inner x should be assigned to 101.

 

ii. If the point of declaration is before = :

auto object = expression;
           ^

Well, it doesn't make any sense because auto has to wait until see the following expression. For example auto x; is invalid.


Update: I need an answer which explains it by the rule point of declaration.

like image 707
masoud Avatar asked Oct 02 '13 09:10

masoud


People also ask

What is the point of auto C++?

The auto keyword in C++ automatically detects and assigns a data type to the variable with which it is used. The compiler analyses the variable's data type by looking at its initialization. It is necessary to initialize the variable when declaring it using the auto keyword.

How do you declare an auto variable?

To use the auto keyword, use it instead of a type to declare a variable, and specify an initialization expression. In addition, you can modify the auto keyword by using specifiers and declarators such as const , volatile , pointer ( * ), reference ( & ), and rvalue reference ( && ).

What is auto datatype?

The auto keyword specifies that the type of the variable that is begin declared will automatically be deduced from its initializer and for functions if their return type is auto then that will be evaluated by return type expression at runtime.

What is keyword auto for in Java?

When a certain variable is declared with the keyword 'auto' and initialized to a certain value, then within the scope of the variable, it is reinitialized upon being called repeatedly. The keyword 'auto' is used extremely rare. A variable is set by default to auto.


2 Answers

auto x = x; // inner x

is ill-formed.

To quote from the C++11 standard (emphasis mine):

7.1.6.4 auto specifier

...

3 Otherwise, the type of the variable is deduced from its initializer. The name of the variable being declared shall not appear in the initializer expression. ...

And so because x after = resolves to the x in auto x (as explained in the question you linked), that above piece of code is ill-formed.

like image 154
Mark Garcia Avatar answered Sep 20 '22 13:09

Mark Garcia


Just like in any other kind of definition, the x on the right-hand side of the initialiser for auto x = x resolves to the local auto x. C++ has always done this (i.e. int x = x compiles but will give you undefined behaviour).

The reason auto x = x fails to compile is because while x is in scope it has no known type yet, and so using it as the initialiser fails because the type can't be deduced from the expression.

Just like any other kind of declaration, x is in scope after its declarator which is auto x.

int x = 10;
int y = 20;
{
    int x = x;  // This is NOT the outer x. This is undefined behaviour (reading an
                // uninitialised variable).
    auto y = y; // This is NOT the outer y. This is a compile error because the type of
                // y is not known.
}
like image 24
Simple Avatar answered Sep 19 '22 13:09

Simple