auto

From RAD Studio
Jump to: navigation, search

Go Up to Keywords, Alphabetical Listing Index


The auto keyword has been redefined in the C++11 standard. Thus, C++Builder supports two different definitions of auto, determined by the standard that each C++ compiler follows.

This page describes both definitions of auto.

C++11 Definition

Category

Storage Class Specifiers (C++)

Syntax

[auto] <data-definition> ;

Description

Use the auto modifier to define a local variable as having a local lifetime.

This is the default for local variables and is rarely used.

Example

void f() {
        auto int x;
}

C++11 Definition

Category

Type Specifiers

Syntax

[auto] <variable_name> = <initializer_expression> ;

Description

Use auto as a simple type specifier that deduces its semantics from the initializer expression.

Example

int f() {
        return 1;
}

int _tmain(int argc, _TCHAR* argv[]) {
        auto x = f();
        return 0;
}

In this example, the type of the variable x is deduced from its initializer expression: f(). Therefore, x would be of type int, the type returned by f().

Code Migration Issue

When Clang-enhanced C++ compilers encounter old auto syntax (that is, auto used as a storage class specifier), they raise the following warning:

[<compiler> Warning] File1.cpp(12): 'auto' storage class specifier is not permitted in C++11, and will not be supported in future releases.

See Also