auto

Summary

Declares a variable using local-variable type inference.

Syntax

auto name1 = value1 [, name2 = value2 [, name3 = value3 ... [, nameN = valueN]]];

Parameters

nameN
The name for the variable. This can be any valid identifier.
valueN
Required. Initializes the variable to a value. This can be any valid expression.

Description

The auto keyword declares a variable with the data type of the initialized expression.

1
2
3
class Foo {}
 
auto foo = new Foo(); // Variable 'foo' has type 'Foo'

auto variables must always be initialized because they retain the type of initialized expression.

auto is typically used as a convenience for instantiating classes. It is not recommended to use auto for primitive types such as bool.

See Also

Share

HTML | BBCode | Direct Link