Skip to content

Commit 3c41d14

Browse files
author
Colin Robertson
authored
Merge pull request MicrosoftDocs#2618 from yecril71pl/patch-1
Three syntaxes for initialising auto-typed variables
2 parents b5d705d + fe56308 commit 3c41d14

1 file changed

Lines changed: 10 additions & 1 deletion

File tree

docs/cpp/auto-cpp.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,16 @@ Conversion cases in which you might not want to use **`auto`**:
4141

4242
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 (**`&&`**). The compiler evaluates the initialization expression and then uses that information to deduce the type of the variable.
4343

44-
The initialization expression can be an assignment (equal-sign syntax), a direct initialization (function-style syntax), an [`operator new`](new-operator-cpp.md) expression, or the initialization expression can be the *for-range-declaration* parameter in a [Range-based `for` Statement (C++)](../cpp/range-based-for-statement-cpp.md) statement. For more information, see [Initializers](../cpp/initializers.md) and the code examples later in this document.
44+
The **`auto`** initialization expression can take several forms:
45+
46+
- Universal initialization syntax, such as `auto a { 42 };`.
47+
- Assignment syntax, such as `auto b = 0;`.
48+
- Universal assignment syntax, which combines the two previous forms, such as `auto c = { 3.14156 };`.
49+
- Direct initialization, or constructor-style syntax, such as `auto d( 1.41421f );`.
50+
51+
For more information, see [Initializers](../cpp/initializers.md) and the code examples later in this document.
52+
53+
When **`auto`** is used to declare the loop parameter in a range-based **`for`** statement, it uses a different initialization syntax, for example `for (auto& i : iterable) do_action(i);`. For more information, see [Range-based `for` Statement (C++)](../cpp/range-based-for-statement-cpp.md).
4554

4655
The **`auto`** keyword is a placeholder for a type, but it is not itself a type. Therefore, the **`auto`** keyword cannot be used in casts or operators such as [`sizeof`](../cpp/sizeof-operator.md) and (for C++/CLI) [`typeid`](../extensions/typeid-cpp-component-extensions.md).
4756

0 commit comments

Comments
 (0)