- tmp/tmp27w_192y/{from.md → to.md} +220 -60
tmp/tmp27w_192y/{from.md → to.md}
RENAMED
|
@@ -46,25 +46,27 @@ exceptions to this rule are the following:
|
|
| 46 |
or `int`.
|
| 47 |
- `short` or `long` can be combined with `int`.
|
| 48 |
- `long` can be combined with `double`.
|
| 49 |
- `long` can be combined with `long`.
|
| 50 |
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
|
|
|
| 55 |
*alias-declaration* ([[dcl.typedef]]) that is not the *declaration* of
|
| 56 |
a *template-declaration*.
|
| 57 |
|
| 58 |
*enum-specifier*s, *class-specifier*s, and *typename-specifier*s are
|
| 59 |
-
discussed in [[dcl.enum]], [[class]], and [[temp.res]],
|
| 60 |
-
The remaining *type-specifier*s are discussed in the rest
|
| 61 |
-
section.
|
| 62 |
|
| 63 |
#### The *cv-qualifiers* <a id="dcl.type.cv">[[dcl.type.cv]]</a>
|
| 64 |
|
| 65 |
-
There are two *cv-qualifiers*, `const` and `volatile`.
|
|
|
|
| 66 |
*cv-qualifier* appears in a *decl-specifier-seq*, the
|
| 67 |
*init-declarator-list* of the declaration shall not be empty.
|
| 68 |
[[basic.type.qualifier]] and [[dcl.fct]] describe how cv-qualifiers
|
| 69 |
affect object and function types. Redundant cv-qualifications are
|
| 70 |
ignored. For example, these could be introduced by typedefs.
|
|
@@ -122,19 +124,22 @@ y.x.j++; // ill-formed: const-qualified member modified
|
|
| 122 |
Y* p = const_cast<Y*>(&y); // cast away const-ness of y
|
| 123 |
p->x.i = 99; // well-formed: mutable member can be modified
|
| 124 |
p->x.j = 99; // undefined: modifies a const member
|
| 125 |
```
|
| 126 |
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
|
|
|
| 130 |
|
| 131 |
`volatile` is a hint to the implementation to avoid aggressive
|
| 132 |
optimization involving the object because the value of the object might
|
| 133 |
-
be changed by means undetectable by an implementation.
|
| 134 |
-
|
| 135 |
-
|
|
|
|
|
|
|
| 136 |
|
| 137 |
#### Simple type specifiers <a id="dcl.type.simple">[[dcl.type.simple]]</a>
|
| 138 |
|
| 139 |
The simple type specifiers are
|
| 140 |
|
|
@@ -168,18 +173,19 @@ type-name:
|
|
| 168 |
```
|
| 169 |
|
| 170 |
``` bnf
|
| 171 |
decltype-specifier:
|
| 172 |
'decltype' '(' expression ')'
|
|
|
|
| 173 |
```
|
| 174 |
|
| 175 |
The `auto` specifier is a placeholder for a type to be deduced (
|
| 176 |
[[dcl.spec.auto]]). The other *simple-type-specifier*s specify either a
|
| 177 |
-
previously-declared
|
| 178 |
-
[[basic.fundamental]]). Table
|
| 179 |
-
|
| 180 |
-
specify.
|
| 181 |
|
| 182 |
**Table: *simple-type-specifier*{s} and the types they specify** <a id="tab:simple.type.specifiers">[tab:simple.type.specifiers]</a>
|
| 183 |
|
| 184 |
| | |
|
| 185 |
| ---------------------- | -------------------------------------- |
|
|
@@ -223,16 +229,16 @@ specify.
|
|
| 223 |
| decltype(*expression*) | the type as defined below |
|
| 224 |
|
| 225 |
|
| 226 |
When multiple *simple-type-specifiers* are allowed, they can be freely
|
| 227 |
intermixed with other *decl-specifiers* in any order. It is
|
| 228 |
-
implementation-defined whether objects of `char` type
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
to be signed; it is redundant in other contexts.
|
| 232 |
|
| 233 |
-
|
|
|
|
| 234 |
|
| 235 |
- if `e` is an unparenthesized *id-expression* or an unparenthesized
|
| 236 |
class member access ([[expr.ref]]), `decltype(e)` is the type of the
|
| 237 |
entity named by `e`. If there is no such entity, or if `e` names a set
|
| 238 |
of overloaded functions, the program is ill-formed;
|
|
@@ -248,16 +254,19 @@ The operand of the `decltype` specifier is an unevaluated operand
|
|
| 248 |
``` cpp
|
| 249 |
const int&& foo();
|
| 250 |
int i;
|
| 251 |
struct A { double x; };
|
| 252 |
const A* a = new A();
|
| 253 |
-
decltype(foo()) x1 =
|
| 254 |
decltype(i) x2; // type is int
|
| 255 |
decltype(a->x) x3; // type is double
|
| 256 |
decltype((a->x)) x4 = x3; // type is const double&
|
| 257 |
```
|
| 258 |
|
|
|
|
|
|
|
|
|
|
| 259 |
in the case where the operand of a *decltype-specifier* is a function
|
| 260 |
call and the return type of the function is a class type, a special
|
| 261 |
rule ([[expr.call]]) ensures that the return type is not required to be
|
| 262 |
complete (as it would be if the call appeared in a sub-expression or
|
| 263 |
outside of a *decltype-specifier*). In this context, the common purpose
|
|
@@ -300,11 +309,12 @@ void r() {
|
|
| 300 |
#### Elaborated type specifiers <a id="dcl.type.elab">[[dcl.type.elab]]</a>
|
| 301 |
|
| 302 |
``` bnf
|
| 303 |
elaborated-type-specifier:
|
| 304 |
class-key attribute-specifier-seqₒₚₜ nested-name-specifierₒₚₜ identifier
|
| 305 |
-
class-key
|
|
|
|
| 306 |
'enum' nested-name-specifierₒₚₜ identifier
|
| 307 |
```
|
| 308 |
|
| 309 |
An *attribute-specifier-seq* shall not appear in an
|
| 310 |
*elaborated-type-specifier* unless the latter is the sole constituent of
|
|
@@ -360,68 +370,94 @@ enum class E { a, b };
|
|
| 360 |
enum E x = E::a; // OK
|
| 361 |
```
|
| 362 |
|
| 363 |
#### `auto` specifier <a id="dcl.spec.auto">[[dcl.spec.auto]]</a>
|
| 364 |
|
| 365 |
-
The `auto` *type-specifier*
|
| 366 |
-
|
| 367 |
-
|
|
|
|
|
|
|
| 368 |
|
| 369 |
-
The
|
| 370 |
-
*
|
| 371 |
-
declarator is valid.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 372 |
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
expression
|
| 376 |
-
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
|
| 380 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 381 |
|
| 382 |
``` cpp
|
| 383 |
auto x = 5; // OK: x has type int
|
| 384 |
const auto *v = &x, u = 6; // OK: v has type const int*, u has type const int
|
| 385 |
static auto y = 0.0; // OK: y has type double
|
| 386 |
auto int r; // error: auto is not a storage-class-specifier
|
|
|
|
|
|
|
|
|
|
| 387 |
```
|
| 388 |
|
| 389 |
-
|
| 390 |
selection statement ([[stmt.select]]) or an iteration statement (
|
| 391 |
[[stmt.iter]]), in the in the or of a ([[expr.new]]), in a
|
| 392 |
*for-range-declaration*, and in declaring a static data member with a
|
| 393 |
*brace-or-equal-initializer* that appears within the of a class
|
| 394 |
definition ([[class.static.data]]).
|
| 395 |
|
| 396 |
-
A program that uses `auto` in a context not
|
| 397 |
-
section is ill-formed.
|
| 398 |
|
| 399 |
-
|
| 400 |
-
|
| 401 |
-
|
| 402 |
-
|
| 403 |
-
|
| 404 |
-
|
| 405 |
-
*
|
| 406 |
-
|
| 407 |
-
|
| 408 |
-
|
| 409 |
-
|
| 410 |
-
|
| 411 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 412 |
|
| 413 |
``` cpp
|
| 414 |
auto x1 = { 1, 2 }; // decltype(x1) is std::initializer_list<int>
|
| 415 |
auto x2 = { 1, 2.0 }; // error: cannot deduce element type
|
| 416 |
```
|
| 417 |
|
| 418 |
-
If the list of declarators contains more than one declarator, the type
|
| 419 |
-
of each declared variable is determined as described above. If the type
|
| 420 |
-
deduced for the template parameter `U` is not the same in each
|
| 421 |
-
deduction, the program is ill-formed.
|
| 422 |
-
|
| 423 |
``` cpp
|
| 424 |
const auto &i = expr;
|
| 425 |
```
|
| 426 |
|
| 427 |
The type of `i` is the deduced type of the parameter `u` in the call
|
|
@@ -429,5 +465,129 @@ The type of `i` is the deduced type of the parameter `u` in the call
|
|
| 429 |
|
| 430 |
``` cpp
|
| 431 |
template <class U> void f(const U& u);
|
| 432 |
```
|
| 433 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
or `int`.
|
| 47 |
- `short` or `long` can be combined with `int`.
|
| 48 |
- `long` can be combined with `double`.
|
| 49 |
- `long` can be combined with `long`.
|
| 50 |
|
| 51 |
+
Except in a declaration of a constructor, destructor, or conversion
|
| 52 |
+
function, at least one *type-specifier* that is not a *cv-qualifier*
|
| 53 |
+
shall appear in a complete *type-specifier-seq* or a complete
|
| 54 |
+
*decl-specifier-seq*.[^3] A *type-specifier-seq* shall not define a
|
| 55 |
+
class or enumeration unless it appears in the *type-id* of an
|
| 56 |
*alias-declaration* ([[dcl.typedef]]) that is not the *declaration* of
|
| 57 |
a *template-declaration*.
|
| 58 |
|
| 59 |
*enum-specifier*s, *class-specifier*s, and *typename-specifier*s are
|
| 60 |
+
discussed in [[dcl.enum]], Clause [[class]], and [[temp.res]],
|
| 61 |
+
respectively. The remaining *type-specifier*s are discussed in the rest
|
| 62 |
+
of this section.
|
| 63 |
|
| 64 |
#### The *cv-qualifiers* <a id="dcl.type.cv">[[dcl.type.cv]]</a>
|
| 65 |
|
| 66 |
+
There are two *cv-qualifiers*, `const` and `volatile`. Each
|
| 67 |
+
*cv-qualifier* shall appear at most once in a *cv-qualifier-seq*. If a
|
| 68 |
*cv-qualifier* appears in a *decl-specifier-seq*, the
|
| 69 |
*init-declarator-list* of the declaration shall not be empty.
|
| 70 |
[[basic.type.qualifier]] and [[dcl.fct]] describe how cv-qualifiers
|
| 71 |
affect object and function types. Redundant cv-qualifications are
|
| 72 |
ignored. For example, these could be introduced by typedefs.
|
|
|
|
| 124 |
Y* p = const_cast<Y*>(&y); // cast away const-ness of y
|
| 125 |
p->x.i = 99; // well-formed: mutable member can be modified
|
| 126 |
p->x.j = 99; // undefined: modifies a const member
|
| 127 |
```
|
| 128 |
|
| 129 |
+
What constitutes an access to an object that has volatile-qualified type
|
| 130 |
+
is implementation-defined. If an attempt is made to refer to an object
|
| 131 |
+
defined with a volatile-qualified type through the use of a glvalue with
|
| 132 |
+
a non-volatile-qualified type, the program behavior is undefined.
|
| 133 |
|
| 134 |
`volatile` is a hint to the implementation to avoid aggressive
|
| 135 |
optimization involving the object because the value of the object might
|
| 136 |
+
be changed by means undetectable by an implementation. Furthermore, for
|
| 137 |
+
some implementations, `volatile` might indicate that special hardware
|
| 138 |
+
instructions are required to access the object. See [[intro.execution]]
|
| 139 |
+
for detailed semantics. In general, the semantics of `volatile` are
|
| 140 |
+
intended to be the same in C++as they are in C.
|
| 141 |
|
| 142 |
#### Simple type specifiers <a id="dcl.type.simple">[[dcl.type.simple]]</a>
|
| 143 |
|
| 144 |
The simple type specifiers are
|
| 145 |
|
|
|
|
| 173 |
```
|
| 174 |
|
| 175 |
``` bnf
|
| 176 |
decltype-specifier:
|
| 177 |
'decltype' '(' expression ')'
|
| 178 |
+
'decltype' '(' 'auto' ')'
|
| 179 |
```
|
| 180 |
|
| 181 |
The `auto` specifier is a placeholder for a type to be deduced (
|
| 182 |
[[dcl.spec.auto]]). The other *simple-type-specifier*s specify either a
|
| 183 |
+
previously-declared type, a type determined from an expression, or one
|
| 184 |
+
of the fundamental types ([[basic.fundamental]]). Table
|
| 185 |
+
[[tab:simple.type.specifiers]] summarizes the valid combinations of
|
| 186 |
+
*simple-type-specifier*s and the types they specify.
|
| 187 |
|
| 188 |
**Table: *simple-type-specifier*{s} and the types they specify** <a id="tab:simple.type.specifiers">[tab:simple.type.specifiers]</a>
|
| 189 |
|
| 190 |
| | |
|
| 191 |
| ---------------------- | -------------------------------------- |
|
|
|
|
| 229 |
| decltype(*expression*) | the type as defined below |
|
| 230 |
|
| 231 |
|
| 232 |
When multiple *simple-type-specifiers* are allowed, they can be freely
|
| 233 |
intermixed with other *decl-specifiers* in any order. It is
|
| 234 |
+
implementation-defined whether objects of `char` type are represented as
|
| 235 |
+
signed or unsigned quantities. The `signed` specifier forces `char`
|
| 236 |
+
objects to be signed; it is redundant in other contexts.
|
|
|
|
| 237 |
|
| 238 |
+
For an expression `e`, the type denoted by `decltype(e)` is defined as
|
| 239 |
+
follows:
|
| 240 |
|
| 241 |
- if `e` is an unparenthesized *id-expression* or an unparenthesized
|
| 242 |
class member access ([[expr.ref]]), `decltype(e)` is the type of the
|
| 243 |
entity named by `e`. If there is no such entity, or if `e` names a set
|
| 244 |
of overloaded functions, the program is ill-formed;
|
|
|
|
| 254 |
``` cpp
|
| 255 |
const int&& foo();
|
| 256 |
int i;
|
| 257 |
struct A { double x; };
|
| 258 |
const A* a = new A();
|
| 259 |
+
decltype(foo()) x1 = 0; // type is const int&&
|
| 260 |
decltype(i) x2; // type is int
|
| 261 |
decltype(a->x) x3; // type is double
|
| 262 |
decltype((a->x)) x4 = x3; // type is const double&
|
| 263 |
```
|
| 264 |
|
| 265 |
+
The rules for determining types involving `decltype(auto)` are specified
|
| 266 |
+
in [[dcl.spec.auto]].
|
| 267 |
+
|
| 268 |
in the case where the operand of a *decltype-specifier* is a function
|
| 269 |
call and the return type of the function is a class type, a special
|
| 270 |
rule ([[expr.call]]) ensures that the return type is not required to be
|
| 271 |
complete (as it would be if the call appeared in a sub-expression or
|
| 272 |
outside of a *decltype-specifier*). In this context, the common purpose
|
|
|
|
| 309 |
#### Elaborated type specifiers <a id="dcl.type.elab">[[dcl.type.elab]]</a>
|
| 310 |
|
| 311 |
``` bnf
|
| 312 |
elaborated-type-specifier:
|
| 313 |
class-key attribute-specifier-seqₒₚₜ nested-name-specifierₒₚₜ identifier
|
| 314 |
+
class-key simple-template-id
|
| 315 |
+
class-key nested-name-specifier 'template'ₒₚₜ simple-template-id
|
| 316 |
'enum' nested-name-specifierₒₚₜ identifier
|
| 317 |
```
|
| 318 |
|
| 319 |
An *attribute-specifier-seq* shall not appear in an
|
| 320 |
*elaborated-type-specifier* unless the latter is the sole constituent of
|
|
|
|
| 370 |
enum E x = E::a; // OK
|
| 371 |
```
|
| 372 |
|
| 373 |
#### `auto` specifier <a id="dcl.spec.auto">[[dcl.spec.auto]]</a>
|
| 374 |
|
| 375 |
+
The `auto` and `decltype(auto)` *type-specifier*s designate a
|
| 376 |
+
placeholder type that will be replaced later, either by deduction from
|
| 377 |
+
an initializer or by explicit specification with a
|
| 378 |
+
*trailing-return-type*. The `auto` *type-specifier* is also used to
|
| 379 |
+
signify that a lambda is a generic lambda.
|
| 380 |
|
| 381 |
+
The placeholder type can appear with a function declarator in the
|
| 382 |
+
*decl-specifier-seq*, *type-specifier-seq*, *conversion-function-id*, or
|
| 383 |
+
*trailing-return-type*, in any context where such a declarator is valid.
|
| 384 |
+
If the function declarator includes a *trailing-return-type* (
|
| 385 |
+
[[dcl.fct]]), that specifies the declared return type of the function.
|
| 386 |
+
If the declared return type of the function contains a placeholder type,
|
| 387 |
+
the return type of the function is deduced from `return` statements in
|
| 388 |
+
the body of the function, if any.
|
| 389 |
|
| 390 |
+
If the `auto` *type-specifier* appears as one of the *decl-specifier*s
|
| 391 |
+
in the *decl-specifier-seq* of a *parameter-declaration* of a
|
| 392 |
+
*lambda-expression*, the lambda is a *generic lambda* (
|
| 393 |
+
[[expr.prim.lambda]]).
|
| 394 |
+
|
| 395 |
+
``` cpp
|
| 396 |
+
auto glambda = [](int i, auto a) { return i; }; // OK: a generic lambda
|
| 397 |
+
```
|
| 398 |
+
|
| 399 |
+
The type of a variable declared using `auto` or `decltype(auto)` is
|
| 400 |
+
deduced from its initializer. This use is allowed when declaring
|
| 401 |
+
variables in a block ([[stmt.block]]), in namespace scope (
|
| 402 |
+
[[basic.scope.namespace]]), and in a ([[stmt.for]]). `auto` or
|
| 403 |
+
`decltype(auto)` shall appear as one of the *decl-specifier*s in the
|
| 404 |
+
*decl-specifier-seq* and the *decl-specifier-seq* shall be followed by
|
| 405 |
+
one or more *init-declarator*s, each of which shall have a non-empty
|
| 406 |
+
*initializer*. In an *initializer* of the form
|
| 407 |
+
|
| 408 |
+
``` cpp
|
| 409 |
+
( expression-list )
|
| 410 |
+
```
|
| 411 |
+
|
| 412 |
+
the *expression-list* shall be a single *assignment-expression*.
|
| 413 |
|
| 414 |
``` cpp
|
| 415 |
auto x = 5; // OK: x has type int
|
| 416 |
const auto *v = &x, u = 6; // OK: v has type const int*, u has type const int
|
| 417 |
static auto y = 0.0; // OK: y has type double
|
| 418 |
auto int r; // error: auto is not a storage-class-specifier
|
| 419 |
+
auto f() -> int; // OK: f returns int
|
| 420 |
+
auto g() { return 0.0; } // OK: g returns double
|
| 421 |
+
auto h(); // OK: h's return type will be deduced when it is defined
|
| 422 |
```
|
| 423 |
|
| 424 |
+
A placeholder type can also be used in declaring a variable in the of a
|
| 425 |
selection statement ([[stmt.select]]) or an iteration statement (
|
| 426 |
[[stmt.iter]]), in the in the or of a ([[expr.new]]), in a
|
| 427 |
*for-range-declaration*, and in declaring a static data member with a
|
| 428 |
*brace-or-equal-initializer* that appears within the of a class
|
| 429 |
definition ([[class.static.data]]).
|
| 430 |
|
| 431 |
+
A program that uses `auto` or `decltype(auto)` in a context not
|
| 432 |
+
explicitly allowed in this section is ill-formed.
|
| 433 |
|
| 434 |
+
When a variable declared using a placeholder type is initialized, or a
|
| 435 |
+
`return` statement occurs in a function declared with a return type that
|
| 436 |
+
contains a placeholder type, the deduced return type or variable type is
|
| 437 |
+
determined from the type of its initializer. In the case of a `return`
|
| 438 |
+
with no operand, the initializer is considered to be `void()`. Let `T`
|
| 439 |
+
be the declared type of the variable or return type of the function. If
|
| 440 |
+
the placeholder is the `auto` *type-specifier*, the deduced type is
|
| 441 |
+
determined using the rules for template argument deduction. If the
|
| 442 |
+
deduction is for a `return` statement and the initializer is a
|
| 443 |
+
*braced-init-list* ([[dcl.init.list]]), the program is ill-formed.
|
| 444 |
+
Otherwise, obtain `P` from `T` by replacing the occurrences of `auto`
|
| 445 |
+
with either a new invented type template parameter `U` or, if the
|
| 446 |
+
initializer is a *braced-init-list*, with `std::initializer_list<U>`.
|
| 447 |
+
Deduce a value for `U` using the rules of template argument deduction
|
| 448 |
+
from a function call ([[temp.deduct.call]]), where `P` is a function
|
| 449 |
+
template parameter type and the initializer is the corresponding
|
| 450 |
+
argument. If the deduction fails, the declaration is ill-formed.
|
| 451 |
+
Otherwise, the type deduced for the variable or return type is obtained
|
| 452 |
+
by substituting the deduced `U` into `P`.
|
| 453 |
|
| 454 |
``` cpp
|
| 455 |
auto x1 = { 1, 2 }; // decltype(x1) is std::initializer_list<int>
|
| 456 |
auto x2 = { 1, 2.0 }; // error: cannot deduce element type
|
| 457 |
```
|
| 458 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 459 |
``` cpp
|
| 460 |
const auto &i = expr;
|
| 461 |
```
|
| 462 |
|
| 463 |
The type of `i` is the deduced type of the parameter `u` in the call
|
|
|
|
| 465 |
|
| 466 |
``` cpp
|
| 467 |
template <class U> void f(const U& u);
|
| 468 |
```
|
| 469 |
|
| 470 |
+
If the placeholder is the `decltype(auto)` *type-specifier*, the
|
| 471 |
+
declared type of the variable or return type of the function shall be
|
| 472 |
+
the placeholder alone. The type deduced for the variable or return type
|
| 473 |
+
is determined as described in [[dcl.type.simple]], as though the
|
| 474 |
+
initializer had been the operand of the `decltype`.
|
| 475 |
+
|
| 476 |
+
``` cpp
|
| 477 |
+
int i;
|
| 478 |
+
int&& f();
|
| 479 |
+
auto x3a = i; // decltype(x3a) is int
|
| 480 |
+
decltype(auto) x3d = i; // decltype(x3d) is int
|
| 481 |
+
auto x4a = (i); // decltype(x4a) is int
|
| 482 |
+
decltype(auto) x4d = (i); // decltype(x4d) is int&
|
| 483 |
+
auto x5a = f(); // decltype(x5a) is int
|
| 484 |
+
decltype(auto) x5d = f(); // decltype(x5d) is int&&
|
| 485 |
+
auto x6a = { 1, 2 }; // decltype(x6a) is std::initializer_list<int>
|
| 486 |
+
decltype(auto) x6d = { 1, 2 }; // error, { 1, 2 } is not an expression
|
| 487 |
+
auto *x7a = &i; // decltype(x7a) is int*
|
| 488 |
+
decltype(auto)*x7d = &i; // error, declared type is not plain decltype(auto)
|
| 489 |
+
```
|
| 490 |
+
|
| 491 |
+
If the *init-declarator-list* contains more than one *init-declarator*,
|
| 492 |
+
they shall all form declarations of variables. The type of each declared
|
| 493 |
+
variable is determined as described above, and if the type that replaces
|
| 494 |
+
the placeholder type is not the same in each deduction, the program is
|
| 495 |
+
ill-formed.
|
| 496 |
+
|
| 497 |
+
``` cpp
|
| 498 |
+
auto x = 5, *y = &x; // OK: auto is int
|
| 499 |
+
auto a = 5, b = { 1, 2 }; // error: different types for auto
|
| 500 |
+
```
|
| 501 |
+
|
| 502 |
+
If a function with a declared return type that contains a placeholder
|
| 503 |
+
type has multiple `return` statements, the return type is deduced for
|
| 504 |
+
each `return` statement. If the type deduced is not the same in each
|
| 505 |
+
deduction, the program is ill-formed.
|
| 506 |
+
|
| 507 |
+
If a function with a declared return type that uses a placeholder type
|
| 508 |
+
has no `return` statements, the return type is deduced as though from a
|
| 509 |
+
`return` statement with no operand at the closing brace of the function
|
| 510 |
+
body.
|
| 511 |
+
|
| 512 |
+
``` cpp
|
| 513 |
+
auto f() { } // OK, return type is void
|
| 514 |
+
auto* g() { } // error, cannot deduce auto* from void()
|
| 515 |
+
```
|
| 516 |
+
|
| 517 |
+
If the type of an entity with an undeduced placeholder type is needed to
|
| 518 |
+
determine the type of an expression, the program is ill-formed. Once a
|
| 519 |
+
`return` statement has been seen in a function, however, the return type
|
| 520 |
+
deduced from that statement can be used in the rest of the function,
|
| 521 |
+
including in other `return` statements.
|
| 522 |
+
|
| 523 |
+
``` cpp
|
| 524 |
+
auto n = n; // error, n's type is unknown
|
| 525 |
+
auto f();
|
| 526 |
+
void g() { &f; } // error, f's return type is unknown
|
| 527 |
+
auto sum(int i) {
|
| 528 |
+
if (i == 1)
|
| 529 |
+
return i; // sum's return type is int
|
| 530 |
+
else
|
| 531 |
+
return sum(i-1)+i; // OK, sum's return type has been deduced
|
| 532 |
+
}
|
| 533 |
+
```
|
| 534 |
+
|
| 535 |
+
Return type deduction for a function template with a placeholder in its
|
| 536 |
+
declared type occurs when the definition is instantiated even if the
|
| 537 |
+
function body contains a `return` statement with a non-type-dependent
|
| 538 |
+
operand. Therefore, any use of a specialization of the function template
|
| 539 |
+
will cause an implicit instantiation. Any errors that arise from this
|
| 540 |
+
instantiation are not in the immediate context of the function type and
|
| 541 |
+
can result in the program being ill-formed.
|
| 542 |
+
|
| 543 |
+
``` cpp
|
| 544 |
+
template <class T> auto f(T t) { return t; } // return type deduced at instantiation time
|
| 545 |
+
typedef decltype(f(1)) fint_t; // instantiates f<int> to deduce return type
|
| 546 |
+
template<class T> auto f(T* t) { return *t; }
|
| 547 |
+
void g() { int (*p)(int*) = &f; } // instantiates both fs to determine return types,
|
| 548 |
+
// chooses second
|
| 549 |
+
```
|
| 550 |
+
|
| 551 |
+
Redeclarations or specializations of a function or function template
|
| 552 |
+
with a declared return type that uses a placeholder type shall also use
|
| 553 |
+
that placeholder, not a deduced type.
|
| 554 |
+
|
| 555 |
+
``` cpp
|
| 556 |
+
auto f();
|
| 557 |
+
auto f() { return 42; } // return type is int
|
| 558 |
+
auto f(); // OK
|
| 559 |
+
int f(); // error, cannot be overloaded with auto f()
|
| 560 |
+
decltype(auto) f(); // error, auto and decltype(auto) don't match
|
| 561 |
+
|
| 562 |
+
template <typename T> auto g(T t) { return t; } // #1
|
| 563 |
+
template auto g(int); // OK, return type is int
|
| 564 |
+
template char g(char); // error, no matching template
|
| 565 |
+
template<> auto g(double); // OK, forward declaration with unknown return type
|
| 566 |
+
|
| 567 |
+
template <class T> T g(T t) { return t; } // OK, not functionally equivalent to #1
|
| 568 |
+
template char g(char); // OK, now there is a matching template
|
| 569 |
+
template auto g(float); // still matches #1
|
| 570 |
+
|
| 571 |
+
void h() { return g(42); } // error, ambiguous
|
| 572 |
+
|
| 573 |
+
template <typename T> struct A {
|
| 574 |
+
friend T frf(T);
|
| 575 |
+
};
|
| 576 |
+
auto frf(int i) { return i; } // not a friend of A<int>
|
| 577 |
+
```
|
| 578 |
+
|
| 579 |
+
A function declared with a return type that uses a placeholder type
|
| 580 |
+
shall not be `virtual` ([[class.virtual]]).
|
| 581 |
+
|
| 582 |
+
An explicit instantiation declaration ([[temp.explicit]]) does not
|
| 583 |
+
cause the instantiation of an entity declared using a placeholder type,
|
| 584 |
+
but it also does not prevent that entity from being instantiated as
|
| 585 |
+
needed to determine its type.
|
| 586 |
+
|
| 587 |
+
``` cpp
|
| 588 |
+
template <typename T> auto f(T t) { return t; }
|
| 589 |
+
extern template auto f(int); // does not instantiate f<int>
|
| 590 |
+
int (*p)(int) = f; // instantiates f<int> to determine its return type, but an explicit
|
| 591 |
+
// instantiation definition is still required somewhere in the program
|
| 592 |
+
```
|
| 593 |
+
|