- tmp/tmpqij0xu72/{from.md → to.md} +1520 -770
tmp/tmpqij0xu72/{from.md → to.md}
RENAMED
|
@@ -10,12 +10,14 @@ declaration-seq:
|
|
| 10 |
```
|
| 11 |
|
| 12 |
``` bnf
|
| 13 |
declaration:
|
| 14 |
block-declaration
|
|
|
|
| 15 |
function-definition
|
| 16 |
template-declaration
|
|
|
|
| 17 |
explicit-instantiation
|
| 18 |
explicit-specialization
|
| 19 |
linkage-specification
|
| 20 |
namespace-definition
|
| 21 |
empty-declaration
|
|
@@ -32,23 +34,30 @@ block-declaration:
|
|
| 32 |
static_assert-declaration
|
| 33 |
alias-declaration
|
| 34 |
opaque-enum-declaration
|
| 35 |
```
|
| 36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
``` bnf
|
| 38 |
alias-declaration:
|
| 39 |
-
'using' identifier attribute-specifier-seqₒₚₜ '=' type-id ';'
|
| 40 |
```
|
| 41 |
|
| 42 |
``` bnf
|
| 43 |
simple-declaration:
|
| 44 |
-
decl-specifier-seq
|
| 45 |
-
attribute-specifier-seq decl-specifier-seq
|
|
|
|
| 46 |
```
|
| 47 |
|
| 48 |
``` bnf
|
| 49 |
static_assert-declaration:
|
|
|
|
| 50 |
'static_assert' '(' constant-expression ',' string-literal ')' ';'
|
| 51 |
```
|
| 52 |
|
| 53 |
``` bnf
|
| 54 |
empty-declaration:
|
|
@@ -58,38 +67,43 @@ empty-declaration:
|
|
| 58 |
``` bnf
|
| 59 |
attribute-declaration:
|
| 60 |
attribute-specifier-seq ';'
|
| 61 |
```
|
| 62 |
|
| 63 |
-
*asm-definition*s are described in [[dcl.asm]], and
|
| 64 |
*linkage-specification*s are described in [[dcl.link]].
|
| 65 |
*Function-definition*s are described in [[dcl.fct.def]] and
|
| 66 |
-
*template-declaration*s are described in Clause
|
| 67 |
-
*Namespace-definition*s are described in [[namespace.def]],
|
| 68 |
*using-declaration*s are described in [[namespace.udecl]] and
|
| 69 |
-
*using-directive*s are described in [[namespace.udir]].
|
| 70 |
|
| 71 |
-
|
| 72 |
|
| 73 |
``` bnf
|
| 74 |
attribute-specifier-seqₒₚₜ decl-specifier-seqₒₚₜ init-declarator-listₒₚₜ ';'
|
| 75 |
```
|
| 76 |
|
| 77 |
is divided into three parts. Attributes are described in [[dcl.attr]].
|
| 78 |
*decl-specifier*s, the principal components of a *decl-specifier-seq*,
|
| 79 |
are described in [[dcl.spec]]. *declarator*s, the components of an
|
| 80 |
*init-declarator-list*, are described in Clause [[dcl.decl]]. The
|
| 81 |
-
*attribute-specifier-seq*
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
``` cpp
|
| 88 |
[[noreturn]] void f [[noreturn]] (); // OK
|
| 89 |
```
|
| 90 |
|
|
|
|
|
|
|
| 91 |
Except where otherwise specified, the meaning of an
|
| 92 |
*attribute-declaration* is *implementation-defined*.
|
| 93 |
|
| 94 |
A declaration occurs in a scope ([[basic.scope]]); the scope rules are
|
| 95 |
summarized in [[basic.lookup]]. A declaration that declares a function
|
|
@@ -111,34 +125,50 @@ omitted only when declaring a class (Clause [[class]]) or enumeration (
|
|
| 111 |
names being declared by the declaration (as *class-name*s, *enum-name*s,
|
| 112 |
or *enumerator*s, depending on the syntax). In such cases, the
|
| 113 |
*decl-specifier-seq* shall introduce one or more names into the program,
|
| 114 |
or shall redeclare a name introduced by a previous declaration.
|
| 115 |
|
|
|
|
|
|
|
| 116 |
``` cpp
|
| 117 |
enum { }; // ill-formed
|
| 118 |
typedef class { }; // ill-formed
|
| 119 |
```
|
| 120 |
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
converted
|
| 125 |
-
|
| 126 |
-
|
|
|
|
|
|
|
| 127 |
except that characters not in the basic source character set (
|
| 128 |
[[lex.charset]]) are not required to appear in the diagnostic message.
|
| 129 |
|
|
|
|
|
|
|
| 130 |
``` cpp
|
| 131 |
-
static_assert(
|
| 132 |
```
|
| 133 |
|
|
|
|
|
|
|
| 134 |
An *empty-declaration* has no effect.
|
| 135 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
Each *init-declarator* in the *init-declarator-list* contains exactly
|
| 137 |
one *declarator-id*, which is the name declared by that
|
| 138 |
*init-declarator* and hence one of the names declared by the
|
| 139 |
-
declaration. The *type-
|
| 140 |
*decl-specifier-seq* and the recursive *declarator* structure of the
|
| 141 |
*init-declarator* describe a type ([[dcl.meaning]]), which is then
|
| 142 |
associated with the name being declared by the *init-declarator*.
|
| 143 |
|
| 144 |
If the *decl-specifier-seq* contains the `typedef` specifier, the
|
|
@@ -155,25 +185,31 @@ declaration are added to a function declaration to make a
|
|
| 155 |
definition unless it contains the `extern` specifier and has no
|
| 156 |
initializer ([[basic.def]]). A definition causes the appropriate amount
|
| 157 |
of storage to be reserved and any appropriate initialization (
|
| 158 |
[[dcl.init]]) to be done.
|
| 159 |
|
| 160 |
-
|
| 161 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 162 |
|
| 163 |
## Specifiers <a id="dcl.spec">[[dcl.spec]]</a>
|
| 164 |
|
| 165 |
The specifiers that can be used in a declaration are
|
| 166 |
|
| 167 |
``` bnf
|
| 168 |
decl-specifier:
|
| 169 |
storage-class-specifier
|
| 170 |
-
type-specifier
|
| 171 |
function-specifier
|
| 172 |
'friend'
|
| 173 |
'typedef'
|
| 174 |
'constexpr'
|
|
|
|
| 175 |
```
|
| 176 |
|
| 177 |
``` bnf
|
| 178 |
decl-specifier-seq:
|
| 179 |
decl-specifier attribute-specifier-seqₒₚₜ
|
|
@@ -184,15 +220,20 @@ The optional *attribute-specifier-seq* in a *decl-specifier-seq*
|
|
| 184 |
appertains to the type determined by the preceding *decl-specifier*s (
|
| 185 |
[[dcl.meaning]]). The *attribute-specifier-seq* affects the type only
|
| 186 |
for the declaration it appears in, not other declarations involving the
|
| 187 |
same type.
|
| 188 |
|
|
|
|
|
|
|
|
|
|
| 189 |
If a *type-name* is encountered while parsing a *decl-specifier-seq*, it
|
| 190 |
is interpreted as part of the *decl-specifier-seq* if and only if there
|
| 191 |
-
is no previous *type-specifier* other than a *cv-qualifier* in
|
| 192 |
-
*decl-specifier-seq*. The sequence shall be self-consistent as
|
| 193 |
-
below.
|
|
|
|
|
|
|
| 194 |
|
| 195 |
``` cpp
|
| 196 |
typedef char* Pc;
|
| 197 |
static Pc; // error: name missing
|
| 198 |
```
|
|
@@ -207,26 +248,35 @@ For another example,
|
|
| 207 |
``` cpp
|
| 208 |
void f(const Pc); // void f(char* const) (not const char*)
|
| 209 |
void g(const int Pc); // void g(const int)
|
| 210 |
```
|
| 211 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 212 |
Since `signed`, `unsigned`, `long`, and `short` by default imply `int`,
|
| 213 |
a *type-name* appearing after one of those specifiers is treated as the
|
| 214 |
name being (re)declared.
|
| 215 |
|
|
|
|
|
|
|
| 216 |
``` cpp
|
| 217 |
void h(unsigned Pc); // void h(unsigned int)
|
| 218 |
void k(unsigned int Pc); // void k(unsigned int)
|
| 219 |
```
|
| 220 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 221 |
### Storage class specifiers <a id="dcl.stc">[[dcl.stc]]</a>
|
| 222 |
|
| 223 |
The storage class specifiers are
|
| 224 |
|
| 225 |
``` bnf
|
| 226 |
storage-class-specifier:
|
| 227 |
-
'register'
|
| 228 |
'static'
|
| 229 |
'thread_local'
|
| 230 |
'extern'
|
| 231 |
'mutable'
|
| 232 |
```
|
|
@@ -235,70 +285,68 @@ At most one *storage-class-specifier* shall appear in a given
|
|
| 235 |
*decl-specifier-seq*, except that `thread_local` may appear with
|
| 236 |
`static` or `extern`. If `thread_local` appears in any declaration of a
|
| 237 |
variable it shall be present in all declarations of that entity. If a
|
| 238 |
*storage-class-specifier* appears in a *decl-specifier-seq*, there can
|
| 239 |
be no `typedef` specifier in the same *decl-specifier-seq* and the
|
| 240 |
-
*init-declarator-list* of the declaration
|
| 241 |
-
an anonymous union declared in a named
|
| 242 |
-
namespace, which shall be declared `static` (
|
| 243 |
-
*storage-class-specifier* applies to the
|
| 244 |
-
*init-declarator* in the list and not to any names
|
| 245 |
-
specifiers. A *storage-class-specifier*
|
| 246 |
-
|
| 247 |
-
instantiation ([[temp.explicit]])
|
|
|
|
| 248 |
|
| 249 |
-
|
| 250 |
-
declared
|
| 251 |
-
[[
|
| 252 |
-
storage duration ([[basic.stc.auto]]). A variable declared without a
|
| 253 |
-
*storage-class-specifier* at block scope or declared as a function
|
| 254 |
-
parameter has automatic storage duration by default.
|
| 255 |
-
|
| 256 |
-
A `register` specifier is a hint to the implementation that the variable
|
| 257 |
-
so declared will be heavily used. The hint can be ignored and in most
|
| 258 |
-
implementations it will be ignored if the address of the variable is
|
| 259 |
-
taken. This use is deprecated (see [[depr.register]]).
|
| 260 |
|
| 261 |
The `thread_local` specifier indicates that the named entity has thread
|
| 262 |
storage duration ([[basic.stc.thread]]). It shall be applied only to
|
| 263 |
the names of variables of namespace or block scope and to the names of
|
| 264 |
static data members. When `thread_local` is applied to a variable of
|
| 265 |
block scope the *storage-class-specifier* `static` is implied if no
|
| 266 |
other *storage-class-specifier* appears in the *decl-specifier-seq*.
|
| 267 |
|
| 268 |
The `static` specifier can be applied only to names of variables and
|
| 269 |
-
functions and to anonymous unions ([[class.union]]). There can be
|
| 270 |
-
`static` function declarations within a block, nor any `static`
|
| 271 |
-
parameters. A `static` specifier used in the declaration of a
|
| 272 |
-
declares the variable to have static storage duration (
|
| 273 |
[[basic.stc.static]]), unless accompanied by the `thread_local`
|
| 274 |
specifier, which declares the variable to have thread storage duration (
|
| 275 |
[[basic.stc.thread]]). A `static` specifier can be used in declarations
|
| 276 |
of class members; [[class.static]] describes its effect. For the
|
| 277 |
linkage of a name declared with a `static` specifier, see
|
| 278 |
[[basic.link]].
|
| 279 |
|
| 280 |
The `extern` specifier can be applied only to the names of variables and
|
| 281 |
functions. The `extern` specifier cannot be used in the declaration of
|
| 282 |
class members or function parameters. For the linkage of a name declared
|
| 283 |
-
with an `extern` specifier, see [[basic.link]].
|
| 284 |
-
|
|
|
|
|
|
|
|
|
|
| 285 |
|
| 286 |
The linkages implied by successive declarations for a given entity shall
|
| 287 |
agree. That is, within a given scope, each declaration declaring the
|
| 288 |
same variable name or the same overloading of a function name shall
|
| 289 |
imply the same linkage. Each function in a given set of overloaded
|
| 290 |
functions can have a different linkage, however.
|
| 291 |
|
|
|
|
|
|
|
| 292 |
``` cpp
|
| 293 |
static char* f(); // f() has internal linkage
|
| 294 |
char* f() // f() still has internal linkage
|
| 295 |
-
{
|
| 296 |
|
| 297 |
char* g(); // g() has external linkage
|
| 298 |
static char* g() // error: inconsistent linkage
|
| 299 |
-
{
|
| 300 |
|
| 301 |
void h();
|
| 302 |
inline void h(); // external linkage
|
| 303 |
|
| 304 |
inline void l();
|
|
@@ -321,14 +369,18 @@ static int c; // error: inconsistent linkage
|
|
| 321 |
|
| 322 |
extern int d; // d has external linkage
|
| 323 |
static int d; // error: inconsistent linkage
|
| 324 |
```
|
| 325 |
|
|
|
|
|
|
|
| 326 |
The name of a declared but undefined class can be used in an `extern`
|
| 327 |
declaration. Such a declaration can only be used in ways that do not
|
| 328 |
require a complete class type.
|
| 329 |
|
|
|
|
|
|
|
| 330 |
``` cpp
|
| 331 |
struct S;
|
| 332 |
extern S a;
|
| 333 |
extern S f();
|
| 334 |
extern void g(S);
|
|
@@ -337,21 +389,27 @@ void h() {
|
|
| 337 |
g(a); // error: S is incomplete
|
| 338 |
f(); // error: S is incomplete
|
| 339 |
}
|
| 340 |
```
|
| 341 |
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 345 |
|
| 346 |
``` cpp
|
| 347 |
class X {
|
| 348 |
mutable const int* p; // OK
|
| 349 |
mutable int* const q; // ill-formed
|
| 350 |
};
|
| 351 |
```
|
| 352 |
|
|
|
|
|
|
|
| 353 |
The `mutable` specifier on a class data member nullifies a `const`
|
| 354 |
specifier applied to the containing class object and permits
|
| 355 |
modification of the mutable class member even though the rest of the
|
| 356 |
object is `const` ([[dcl.type.cv]]).
|
| 357 |
|
|
@@ -359,48 +417,14 @@ object is `const` ([[dcl.type.cv]]).
|
|
| 359 |
|
| 360 |
can be used only in function declarations.
|
| 361 |
|
| 362 |
``` bnf
|
| 363 |
function-specifier:
|
| 364 |
-
'inline'
|
| 365 |
'virtual'
|
| 366 |
'explicit'
|
| 367 |
```
|
| 368 |
|
| 369 |
-
A function declaration ([[dcl.fct]], [[class.mfct]], [[class.friend]])
|
| 370 |
-
with an `inline` specifier declares an *inline function*. The inline
|
| 371 |
-
specifier indicates to the implementation that inline substitution of
|
| 372 |
-
the function body at the point of call is to be preferred to the usual
|
| 373 |
-
function call mechanism. An implementation is not required to perform
|
| 374 |
-
this inline substitution at the point of call; however, even if this
|
| 375 |
-
inline substitution is omitted, the other rules for inline functions
|
| 376 |
-
defined by [[dcl.fct.spec]] shall still be respected.
|
| 377 |
-
|
| 378 |
-
A function defined within a class definition is an inline function. The
|
| 379 |
-
`inline` specifier shall not appear on a block scope function
|
| 380 |
-
declaration.[^2] If the `inline` specifier is used in a friend
|
| 381 |
-
declaration, that declaration shall be a definition or the function
|
| 382 |
-
shall have previously been declared inline.
|
| 383 |
-
|
| 384 |
-
An inline function shall be defined in every translation unit in which
|
| 385 |
-
it is odr-used and shall have exactly the same definition in every
|
| 386 |
-
case ([[basic.def.odr]]). A call to the inline function may be
|
| 387 |
-
encountered before its definition appears in the translation unit. If
|
| 388 |
-
the definition of a function appears in a translation unit before its
|
| 389 |
-
first declaration as inline, the program is ill-formed. If a function
|
| 390 |
-
with external linkage is declared inline in one translation unit, it
|
| 391 |
-
shall be declared inline in all translation units in which it appears;
|
| 392 |
-
no diagnostic is required. An `inline` function with external linkage
|
| 393 |
-
shall have the same address in all translation units. A `static` local
|
| 394 |
-
variable in an `extern` `inline` function always refers to the same
|
| 395 |
-
object. A string literal in the body of an `extern` `inline` function is
|
| 396 |
-
the same object in different translation units. A string literal
|
| 397 |
-
appearing in a default argument is not in the body of an inline function
|
| 398 |
-
merely because the expression is used in a function call from that
|
| 399 |
-
inline function. A type defined within the body of an `extern inline`
|
| 400 |
-
function is the same type in every translation unit.
|
| 401 |
-
|
| 402 |
The `virtual` specifier shall be used only in the initial declaration of
|
| 403 |
a non-static class member function; see [[class.virtual]].
|
| 404 |
|
| 405 |
The `explicit` specifier shall be used only in the declaration of a
|
| 406 |
constructor or conversion function within its class definition; see
|
|
@@ -410,14 +434,16 @@ constructor or conversion function within its class definition; see
|
|
| 410 |
|
| 411 |
Declarations containing the *decl-specifier* `typedef` declare
|
| 412 |
identifiers that can be used later for naming fundamental (
|
| 413 |
[[basic.fundamental]]) or compound ([[basic.compound]]) types. The
|
| 414 |
`typedef` specifier shall not be combined in a *decl-specifier-seq* with
|
| 415 |
-
any other kind of specifier except a *type-specifier
|
| 416 |
-
be used in the *decl-specifier-seq* of a
|
| 417 |
-
[[dcl.fct]]) nor in the *decl-specifier-seq*
|
| 418 |
-
*function-definition* ([[dcl.fct.def]]).
|
|
|
|
|
|
|
| 419 |
|
| 420 |
``` bnf
|
| 421 |
typedef-name:
|
| 422 |
identifier
|
| 423 |
```
|
|
@@ -426,11 +452,15 @@ A name declared with the `typedef` specifier becomes a *typedef-name*.
|
|
| 426 |
Within the scope of its declaration, a *typedef-name* is syntactically
|
| 427 |
equivalent to a keyword and names the type associated with the
|
| 428 |
identifier in the way described in Clause [[dcl.decl]]. A
|
| 429 |
*typedef-name* is thus a synonym for another type. A *typedef-name* does
|
| 430 |
not introduce a new type the way a class declaration ([[class.name]])
|
| 431 |
-
or enum declaration does.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 432 |
|
| 433 |
``` cpp
|
| 434 |
typedef int MILES, *KLICKSP;
|
| 435 |
```
|
| 436 |
|
|
@@ -440,88 +470,121 @@ the constructions
|
|
| 440 |
MILES distance;
|
| 441 |
extern KLICKSP metricp;
|
| 442 |
```
|
| 443 |
|
| 444 |
are all correct declarations; the type of `distance` is `int` and that
|
| 445 |
-
of `metricp` is “pointer to `int`
|
|
|
|
|
|
|
| 446 |
|
| 447 |
A *typedef-name* can also be introduced by an *alias-declaration*. The
|
| 448 |
*identifier* following the `using` keyword becomes a *typedef-name* and
|
| 449 |
the optional *attribute-specifier-seq* following the *identifier*
|
| 450 |
-
appertains to that *typedef-name*.
|
| 451 |
-
were introduced by the `typedef` specifier. In
|
| 452 |
-
|
|
|
|
|
|
|
| 453 |
|
| 454 |
``` cpp
|
| 455 |
using handler_t = void (*)(int);
|
| 456 |
extern handler_t ignore;
|
| 457 |
extern void (*ignore)(int); // redeclare ignore
|
| 458 |
using cell = pair<void*, cell*>; // ill-formed
|
| 459 |
```
|
| 460 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 461 |
In a given non-class scope, a `typedef` specifier can be used to
|
| 462 |
redefine the name of any type declared in that scope to refer to the
|
| 463 |
type to which it already refers.
|
| 464 |
|
|
|
|
|
|
|
| 465 |
``` cpp
|
| 466 |
-
typedef struct s {
|
| 467 |
typedef int I;
|
| 468 |
typedef int I;
|
| 469 |
typedef I I;
|
| 470 |
```
|
| 471 |
|
|
|
|
|
|
|
| 472 |
In a given class scope, a `typedef` specifier can be used to redefine
|
| 473 |
any *class-name* declared in that scope that is not also a
|
| 474 |
*typedef-name* to refer to the type to which it already refers.
|
| 475 |
|
|
|
|
|
|
|
| 476 |
``` cpp
|
| 477 |
struct S {
|
| 478 |
typedef struct A { } A; // OK
|
| 479 |
typedef struct B B; // OK
|
| 480 |
typedef A A; // error
|
| 481 |
};
|
| 482 |
```
|
| 483 |
|
|
|
|
|
|
|
| 484 |
If a `typedef` specifier is used to redefine in a given scope an entity
|
| 485 |
that can be referenced using an *elaborated-type-specifier*, the entity
|
| 486 |
can continue to be referenced by an *elaborated-type-specifier* or as an
|
| 487 |
enumeration or class name in an enumeration or class definition
|
| 488 |
respectively.
|
| 489 |
|
|
|
|
|
|
|
| 490 |
``` cpp
|
| 491 |
struct S;
|
| 492 |
typedef struct S S;
|
| 493 |
int main() {
|
| 494 |
struct S* p; // OK
|
| 495 |
}
|
| 496 |
struct S { }; // OK
|
| 497 |
```
|
| 498 |
|
|
|
|
|
|
|
| 499 |
In a given scope, a `typedef` specifier shall not be used to redefine
|
| 500 |
the name of any type declared in that scope to refer to a different
|
| 501 |
type.
|
| 502 |
|
|
|
|
|
|
|
| 503 |
``` cpp
|
| 504 |
-
class complex {
|
| 505 |
typedef int complex; // error: redefinition
|
| 506 |
```
|
| 507 |
|
|
|
|
|
|
|
| 508 |
Similarly, in a given scope, a class or enumeration shall not be
|
| 509 |
declared with the same name as a *typedef-name* that is declared in that
|
| 510 |
scope and refers to a type other than the class or enumeration itself.
|
| 511 |
|
|
|
|
|
|
|
| 512 |
``` cpp
|
| 513 |
typedef int complex;
|
| 514 |
-
class complex {
|
| 515 |
```
|
| 516 |
|
| 517 |
-
|
| 518 |
-
|
| 519 |
-
|
| 520 |
-
|
| 521 |
-
|
| 522 |
-
[[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 523 |
|
| 524 |
``` cpp
|
| 525 |
struct S {
|
| 526 |
S();
|
| 527 |
~S();
|
|
@@ -531,35 +594,47 @@ typedef struct S T;
|
|
| 531 |
|
| 532 |
S a = T(); // OK
|
| 533 |
struct T * p; // error
|
| 534 |
```
|
| 535 |
|
|
|
|
|
|
|
| 536 |
If the typedef declaration defines an unnamed class (or enum), the first
|
| 537 |
*typedef-name* declared by the declaration to be that class type (or
|
| 538 |
enum type) is used to denote the class type (or enum type) for linkage
|
| 539 |
purposes only ([[basic.link]]).
|
| 540 |
|
|
|
|
|
|
|
| 541 |
``` cpp
|
| 542 |
typedef struct { } *ps, S; // S is the class name for linkage purposes
|
| 543 |
```
|
| 544 |
|
|
|
|
|
|
|
| 545 |
### The `friend` specifier <a id="dcl.friend">[[dcl.friend]]</a>
|
| 546 |
|
| 547 |
The `friend` specifier is used to specify access to class members; see
|
| 548 |
[[class.friend]].
|
| 549 |
|
| 550 |
### The `constexpr` specifier <a id="dcl.constexpr">[[dcl.constexpr]]</a>
|
| 551 |
|
| 552 |
The `constexpr` specifier shall be applied only to the definition of a
|
| 553 |
-
variable or variable template
|
| 554 |
-
template
|
| 555 |
-
|
| 556 |
-
|
| 557 |
-
|
| 558 |
-
|
| 559 |
-
|
| 560 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 561 |
|
| 562 |
``` cpp
|
| 563 |
constexpr void square(int &x); // OK: declaration
|
| 564 |
constexpr int bufsz = 1024; // OK: definition
|
| 565 |
constexpr struct pixel { // error: pixel is a type
|
|
@@ -581,31 +656,34 @@ int next(constexpr int x) { // error: not for parameters
|
|
| 581 |
return x + 1;
|
| 582 |
}
|
| 583 |
extern constexpr int memsz; // error: not a definition
|
| 584 |
```
|
| 585 |
|
|
|
|
|
|
|
| 586 |
A `constexpr` specifier used in the declaration of a function that is
|
| 587 |
not a constructor declares that function to be a *constexpr function*.
|
| 588 |
Similarly, a `constexpr` specifier used in a constructor declaration
|
| 589 |
-
declares that constructor to be a *constexpr constructor*.
|
| 590 |
-
functions and `constexpr` constructors are implicitly `inline` (
|
| 591 |
-
[[dcl.fct.spec]]).
|
| 592 |
|
| 593 |
-
The definition of a
|
| 594 |
-
|
| 595 |
|
| 596 |
- it shall not be virtual ([[class.virtual]]);
|
| 597 |
- its return type shall be a literal type;
|
| 598 |
- each of its parameter types shall be a literal type;
|
| 599 |
- its *function-body* shall be `= delete`, `= default`, or a
|
| 600 |
*compound-statement* that does not contain
|
| 601 |
- an *asm-definition*,
|
| 602 |
- a `goto` statement,
|
|
|
|
| 603 |
- a *try-block*, or
|
| 604 |
- a definition of a variable of non-literal type or of static or
|
| 605 |
thread storage duration or for which no initialization is performed.
|
| 606 |
|
|
|
|
|
|
|
| 607 |
``` cpp
|
| 608 |
constexpr int square(int x)
|
| 609 |
{ return x * x; } // OK
|
| 610 |
constexpr long long_max()
|
| 611 |
{ return 2147483647; } // OK
|
|
@@ -629,51 +707,60 @@ constexpr int g(int x, int n) { // OK
|
|
| 629 |
while (--n > 0) r *= x;
|
| 630 |
return r;
|
| 631 |
}
|
| 632 |
```
|
| 633 |
|
| 634 |
-
|
| 635 |
-
|
|
|
|
|
|
|
| 636 |
|
| 637 |
- the class shall not have any virtual base classes;
|
| 638 |
- each of the parameter types shall be a literal type;
|
| 639 |
-
- its *function-body* shall not be a *function-try-block*
|
| 640 |
|
| 641 |
In addition, either its *function-body* shall be `= delete`, or it shall
|
| 642 |
-
satisfy the following
|
| 643 |
|
| 644 |
- either its *function-body* shall be `= default`, or the
|
| 645 |
*compound-statement* of its *function-body* shall satisfy the
|
| 646 |
-
|
| 647 |
-
- every non-variant non-static data member and base class
|
| 648 |
shall be initialized ([[class.base.init]]);
|
| 649 |
- if the class is a union having variant members ([[class.union]]),
|
| 650 |
exactly one of them shall be initialized;
|
| 651 |
- if the class is a union-like class, but is not a union, for each of
|
| 652 |
its anonymous union members having variant members, exactly one of
|
| 653 |
them shall be initialized;
|
| 654 |
- for a non-delegating constructor, every constructor selected to
|
| 655 |
-
initialize non-static data members and base class
|
| 656 |
-
a
|
| 657 |
- for a delegating constructor, the target constructor shall be a
|
| 658 |
-
|
|
|
|
|
|
|
| 659 |
|
| 660 |
``` cpp
|
| 661 |
struct Length {
|
| 662 |
constexpr explicit Length(int i = 0) : val(i) { }
|
| 663 |
private:
|
| 664 |
int val;
|
| 665 |
};
|
| 666 |
```
|
| 667 |
|
| 668 |
-
|
| 669 |
-
|
| 670 |
-
|
| 671 |
-
|
| 672 |
-
|
|
|
|
|
|
|
|
|
|
| 673 |
required.
|
| 674 |
|
|
|
|
|
|
|
| 675 |
``` cpp
|
| 676 |
constexpr int f(bool b)
|
| 677 |
{ return b ? throw 0 : 0; } // OK
|
| 678 |
constexpr int f() { return f(true); } // ill-formed, no diagnostic required
|
| 679 |
|
|
@@ -688,66 +775,113 @@ struct D : B {
|
|
| 688 |
constexpr D() : B(global) { } // ill-formed, no diagnostic required
|
| 689 |
// lvalue-to-rvalue conversion on non-constant global
|
| 690 |
};
|
| 691 |
```
|
| 692 |
|
| 693 |
-
|
|
|
|
|
|
|
| 694 |
template or member function of a class template would fail to satisfy
|
| 695 |
-
the requirements for a
|
| 696 |
-
|
| 697 |
-
|
| 698 |
-
|
| 699 |
-
|
| 700 |
-
|
| 701 |
-
|
| 702 |
-
|
| 703 |
-
A call to a
|
| 704 |
-
|
| 705 |
-
|
| 706 |
-
|
| 707 |
-
|
| 708 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 709 |
|
| 710 |
``` cpp
|
| 711 |
constexpr int bar(int x, int y) // OK
|
| 712 |
{ return x + y + x*y; }
|
| 713 |
// ...
|
| 714 |
int bar(int x, int y) // error: redefinition of bar
|
| 715 |
{ return x * 2 + 3 * y; }
|
| 716 |
```
|
| 717 |
|
|
|
|
|
|
|
| 718 |
A `constexpr` specifier used in an object declaration declares the
|
| 719 |
object as `const`. Such an object shall have literal type and shall be
|
| 720 |
-
initialized.
|
| 721 |
-
be a constant expression (
|
| 722 |
-
|
| 723 |
-
|
| 724 |
-
|
| 725 |
-
expressions and each constructor call used for the initialization is
|
| 726 |
-
part of such a full-expression.
|
| 727 |
|
| 728 |
``` cpp
|
| 729 |
struct pixel {
|
| 730 |
int x, y;
|
| 731 |
};
|
| 732 |
constexpr pixel ur = { 1294, 1024 }; // OK
|
| 733 |
constexpr pixel origin; // error: initializer missing
|
| 734 |
```
|
| 735 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 736 |
### Type specifiers <a id="dcl.type">[[dcl.type]]</a>
|
| 737 |
|
| 738 |
The type-specifiers are
|
| 739 |
|
| 740 |
``` bnf
|
| 741 |
type-specifier:
|
| 742 |
-
trailing-type-specifier
|
| 743 |
-
class-specifier
|
| 744 |
-
enum-specifier
|
| 745 |
-
```
|
| 746 |
-
|
| 747 |
-
``` bnf
|
| 748 |
-
trailing-type-specifier:
|
| 749 |
simple-type-specifier
|
| 750 |
elaborated-type-specifier
|
| 751 |
typename-specifier
|
| 752 |
cv-qualifier
|
| 753 |
```
|
|
@@ -757,74 +891,91 @@ type-specifier-seq:
|
|
| 757 |
type-specifier attribute-specifier-seqₒₚₜ
|
| 758 |
type-specifier type-specifier-seq
|
| 759 |
```
|
| 760 |
|
| 761 |
``` bnf
|
| 762 |
-
|
| 763 |
-
|
| 764 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 765 |
```
|
| 766 |
|
| 767 |
The optional *attribute-specifier-seq* in a *type-specifier-seq* or a
|
| 768 |
-
*
|
| 769 |
-
preceding *type-specifier*s (
|
| 770 |
-
*attribute-specifier-seq* affects the type only
|
| 771 |
-
appears in, not other declarations involving the
|
|
|
|
| 772 |
|
| 773 |
-
As a general rule, at most one *type-specifier* is allowed in
|
| 774 |
-
complete *decl-specifier-seq* of a *declaration* or in a
|
| 775 |
-
*type-specifier-seq*
|
| 776 |
-
exceptions to this rule are
|
|
|
|
| 777 |
|
| 778 |
- `const` can be combined with any type specifier except itself.
|
| 779 |
- `volatile` can be combined with any type specifier except itself.
|
| 780 |
- `signed` or `unsigned` can be combined with `char`, `long`, `short`,
|
| 781 |
or `int`.
|
| 782 |
- `short` or `long` can be combined with `int`.
|
| 783 |
- `long` can be combined with `double`.
|
| 784 |
- `long` can be combined with `long`.
|
| 785 |
|
| 786 |
Except in a declaration of a constructor, destructor, or conversion
|
| 787 |
-
function, at least one *type-specifier* that is not a
|
| 788 |
-
shall appear in a complete *type-specifier-seq* or a
|
| 789 |
-
*decl-specifier-seq*.[^3]
|
| 790 |
-
class or enumeration unless it appears in the *type-id* of an
|
| 791 |
-
*alias-declaration* ([[dcl.typedef]]) that is not the *declaration* of
|
| 792 |
-
a *template-declaration*.
|
| 793 |
|
| 794 |
-
*enum-specifier*s, *class-specifier*s, and
|
| 795 |
-
discussed in [[dcl.enum]], Clause [[class]],
|
| 796 |
-
respectively. The remaining *type-specifier*s are
|
| 797 |
-
of this section.
|
| 798 |
|
| 799 |
-
#### The *cv-
|
| 800 |
|
| 801 |
-
There are two *cv-
|
| 802 |
*cv-qualifier* shall appear at most once in a *cv-qualifier-seq*. If a
|
| 803 |
*cv-qualifier* appears in a *decl-specifier-seq*, the
|
| 804 |
-
*init-declarator-list* of the declaration
|
| 805 |
-
|
| 806 |
-
|
| 807 |
-
|
| 808 |
-
|
| 809 |
-
|
| 810 |
-
|
| 811 |
-
|
| 812 |
-
|
| 813 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 814 |
|
| 815 |
A pointer or reference to a cv-qualified type need not actually point or
|
| 816 |
refer to a cv-qualified object, but it is treated as if it does; a
|
| 817 |
const-qualified access path cannot be used to modify an object even if
|
| 818 |
the object referenced is a non-const object and can be modified through
|
| 819 |
-
some other access path.
|
| 820 |
-
|
|
|
|
|
|
|
|
|
|
| 821 |
|
| 822 |
Except that any class member declared `mutable` ([[dcl.stc]]) can be
|
| 823 |
modified, any attempt to modify a `const` object during its lifetime (
|
| 824 |
[[basic.life]]) results in undefined behavior.
|
| 825 |
|
|
|
|
|
|
|
| 826 |
``` cpp
|
| 827 |
const int ci = 3; // cv-qualified (initialized as required)
|
| 828 |
ci = 4; // ill-formed: attempt to modify const
|
| 829 |
|
| 830 |
int i = 2; // not cv-qualified
|
|
@@ -839,11 +990,11 @@ ip = const_cast<int*>(cip); // cast needed to convert const int* to int*
|
|
| 839 |
const int* ciq = new const int (3); // initialized as required
|
| 840 |
int* iq = const_cast<int*>(ciq); // cast required
|
| 841 |
*iq = 4; // undefined: modifies a const object
|
| 842 |
```
|
| 843 |
|
| 844 |
-
For another example
|
| 845 |
|
| 846 |
``` cpp
|
| 847 |
struct X {
|
| 848 |
mutable int i;
|
| 849 |
int j;
|
|
@@ -859,31 +1010,35 @@ y.x.j++; // ill-formed: const-qualified member modified
|
|
| 859 |
Y* p = const_cast<Y*>(&y); // cast away const-ness of y
|
| 860 |
p->x.i = 99; // well-formed: mutable member can be modified
|
| 861 |
p->x.j = 99; // undefined: modifies a const member
|
| 862 |
```
|
| 863 |
|
| 864 |
-
|
| 865 |
-
is implementation-defined. If an attempt is made to refer to an object
|
| 866 |
-
defined with a volatile-qualified type through the use of a glvalue with
|
| 867 |
-
a non-volatile-qualified type, the program behavior is undefined.
|
| 868 |
|
| 869 |
-
|
| 870 |
-
|
| 871 |
-
|
| 872 |
-
|
| 873 |
-
|
| 874 |
-
|
| 875 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 876 |
|
| 877 |
#### Simple type specifiers <a id="dcl.type.simple">[[dcl.type.simple]]</a>
|
| 878 |
|
| 879 |
The simple type specifiers are
|
| 880 |
|
| 881 |
``` bnf
|
| 882 |
simple-type-specifier:
|
| 883 |
nested-name-specifierₒₚₜ type-name
|
| 884 |
nested-name-specifier 'template' simple-template-id
|
|
|
|
| 885 |
'char'
|
| 886 |
'char16_t'
|
| 887 |
'char32_t'
|
| 888 |
'wchar_t'
|
| 889 |
'bool'
|
|
@@ -911,23 +1066,28 @@ type-name:
|
|
| 911 |
decltype-specifier:
|
| 912 |
'decltype' '(' expression ')'
|
| 913 |
'decltype' '(' 'auto' ')'
|
| 914 |
```
|
| 915 |
|
| 916 |
-
The `auto`
|
| 917 |
-
[[dcl.spec.auto]]).
|
|
|
|
|
|
|
|
|
|
|
|
|
| 918 |
previously-declared type, a type determined from an expression, or one
|
| 919 |
of the fundamental types ([[basic.fundamental]]). Table
|
| 920 |
[[tab:simple.type.specifiers]] summarizes the valid combinations of
|
| 921 |
*simple-type-specifier*s and the types they specify.
|
| 922 |
|
| 923 |
**Table: *simple-type-specifier*{s} and the types they specify** <a id="tab:simple.type.specifiers">[tab:simple.type.specifiers]</a>
|
| 924 |
|
| 925 |
-
|
|
| 926 |
| ---------------------- | -------------------------------------- |
|
| 927 |
| *type-name* | the type named |
|
| 928 |
| *simple-template-id* | the type as defined in~ [[temp.names]] |
|
|
|
|
| 929 |
| char | ``char'' |
|
| 930 |
| unsigned char | ``unsigned char'' |
|
| 931 |
| signed char | ``signed char'' |
|
| 932 |
| char16_t | ``char16_t'' |
|
| 933 |
| char32_t | ``char32_t'' |
|
|
@@ -959,90 +1119,109 @@ of the fundamental types ([[basic.fundamental]]). Table
|
|
| 959 |
| float | ``float'' |
|
| 960 |
| double | ``double'' |
|
| 961 |
| long double | ``long double'' |
|
| 962 |
| void | ``void'' |
|
| 963 |
| auto | placeholder for a type to be deduced |
|
|
|
|
| 964 |
| decltype(*expression*) | the type as defined below |
|
| 965 |
|
| 966 |
|
| 967 |
-
When multiple *simple-type-
|
| 968 |
-
intermixed with other *decl-
|
| 969 |
-
|
| 970 |
-
|
| 971 |
-
|
|
|
|
|
|
|
| 972 |
|
| 973 |
For an expression `e`, the type denoted by `decltype(e)` is defined as
|
| 974 |
follows:
|
| 975 |
|
| 976 |
-
- if `e` is an unparenthesized *id-expression*
|
| 977 |
-
|
| 978 |
-
|
| 979 |
-
|
|
|
|
|
|
|
|
|
|
| 980 |
- otherwise, if `e` is an xvalue, `decltype(e)` is `T&&`, where `T` is
|
| 981 |
the type of `e`;
|
| 982 |
- otherwise, if `e` is an lvalue, `decltype(e)` is `T&`, where `T` is
|
| 983 |
the type of `e`;
|
| 984 |
- otherwise, `decltype(e)` is the type of `e`.
|
| 985 |
|
| 986 |
The operand of the `decltype` specifier is an unevaluated operand
|
| 987 |
(Clause [[expr]]).
|
| 988 |
|
|
|
|
|
|
|
| 989 |
``` cpp
|
| 990 |
const int&& foo();
|
| 991 |
int i;
|
| 992 |
struct A { double x; };
|
| 993 |
const A* a = new A();
|
| 994 |
-
decltype(foo()) x1 =
|
| 995 |
decltype(i) x2; // type is int
|
| 996 |
decltype(a->x) x3; // type is double
|
| 997 |
decltype((a->x)) x4 = x3; // type is const double&
|
| 998 |
```
|
| 999 |
|
| 1000 |
-
|
| 1001 |
-
|
| 1002 |
-
|
| 1003 |
-
|
| 1004 |
-
|
| 1005 |
-
|
| 1006 |
-
|
| 1007 |
-
|
| 1008 |
-
|
| 1009 |
-
|
| 1010 |
-
|
| 1011 |
-
|
| 1012 |
-
|
| 1013 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1014 |
|
| 1015 |
``` cpp
|
| 1016 |
template<class T> struct A { ~A() = delete; };
|
| 1017 |
template<class T> auto h()
|
| 1018 |
-> A<T>;
|
| 1019 |
template<class T> auto i(T) // identity
|
| 1020 |
-> T;
|
| 1021 |
template<class T> auto f(T) // #1
|
| 1022 |
-
-> decltype(i(h<T>())); // forces completion of A<T> and implicitly uses
|
| 1023 |
-
//
|
| 1024 |
-
//
|
| 1025 |
-
// as a result of the use of i().)
|
| 1026 |
template<class T> auto f(T) // #2
|
| 1027 |
-> void;
|
| 1028 |
auto g() -> void {
|
| 1029 |
-
f(42); // OK: calls #2. (#1 is not a viable candidate: type
|
| 1030 |
-
//
|
| 1031 |
-
//
|
| 1032 |
}
|
| 1033 |
template<class T> auto q(T)
|
| 1034 |
-
-> decltype((h<T>())); // does not force completion of A<T>; A<T>::~A() is
|
| 1035 |
-
//
|
| 1036 |
void r() {
|
| 1037 |
-
q(42); // Error: deduction against q succeeds, so overload resolution
|
| 1038 |
-
//
|
| 1039 |
// The return type is A<int>, so a temporary is introduced and its
|
| 1040 |
// destructor is used, so the program is ill-formed.
|
| 1041 |
}
|
| 1042 |
```
|
| 1043 |
|
|
|
|
|
|
|
| 1044 |
#### Elaborated type specifiers <a id="dcl.type.elab">[[dcl.type.elab]]</a>
|
| 1045 |
|
| 1046 |
``` bnf
|
| 1047 |
elaborated-type-specifier:
|
| 1048 |
class-key attribute-specifier-seqₒₚₜ nested-name-specifierₒₚₜ identifier
|
|
@@ -1076,20 +1255,26 @@ class whenever it is named.
|
|
| 1076 |
resolves to a *class-name* or *enum-name*, the
|
| 1077 |
*elaborated-type-specifier* introduces it into the declaration the same
|
| 1078 |
way a *simple-type-specifier* introduces its *type-name*. If the
|
| 1079 |
*identifier* resolves to a *typedef-name* or the *simple-template-id*
|
| 1080 |
resolves to an alias template specialization, the
|
| 1081 |
-
*elaborated-type-specifier* is ill-formed.
|
| 1082 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1083 |
|
| 1084 |
``` cpp
|
| 1085 |
friend class T;
|
| 1086 |
```
|
| 1087 |
|
| 1088 |
is ill-formed. However, the similar declaration `friend T;` is allowed (
|
| 1089 |
[[class.friend]]).
|
| 1090 |
|
|
|
|
|
|
|
| 1091 |
The *class-key* or `enum` keyword present in the
|
| 1092 |
*elaborated-type-specifier* shall agree in kind with the declaration to
|
| 1093 |
which the name in the *elaborated-type-specifier* refers. This rule also
|
| 1094 |
applies to the form of *elaborated-type-specifier* that declares a
|
| 1095 |
*class-name* or `friend` class since it can be construed as referring to
|
|
@@ -1098,164 +1283,130 @@ the `enum` keyword shall be used to refer to an enumeration (
|
|
| 1098 |
[[dcl.enum]]), the `union` *class-key* shall be used to refer to a union
|
| 1099 |
(Clause [[class]]), and either the `class` or `struct` *class-key*
|
| 1100 |
shall be used to refer to a class (Clause [[class]]) declared using the
|
| 1101 |
`class` or `struct` *class-key*.
|
| 1102 |
|
|
|
|
|
|
|
| 1103 |
``` cpp
|
| 1104 |
enum class E { a, b };
|
| 1105 |
enum E x = E::a; // OK
|
| 1106 |
```
|
| 1107 |
|
| 1108 |
-
|
| 1109 |
|
| 1110 |
-
The `auto`
|
| 1111 |
-
|
| 1112 |
-
|
| 1113 |
-
|
| 1114 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1115 |
|
| 1116 |
The placeholder type can appear with a function declarator in the
|
| 1117 |
*decl-specifier-seq*, *type-specifier-seq*, *conversion-function-id*, or
|
| 1118 |
*trailing-return-type*, in any context where such a declarator is valid.
|
| 1119 |
If the function declarator includes a *trailing-return-type* (
|
| 1120 |
-
[[dcl.fct]]), that specifies the declared return
|
| 1121 |
-
|
| 1122 |
-
the return type of the function
|
| 1123 |
-
the
|
|
|
|
|
|
|
| 1124 |
|
| 1125 |
If the `auto` *type-specifier* appears as one of the *decl-specifier*s
|
| 1126 |
in the *decl-specifier-seq* of a *parameter-declaration* of a
|
| 1127 |
*lambda-expression*, the lambda is a *generic lambda* (
|
| 1128 |
-
[[expr.prim.lambda]]).
|
|
|
|
|
|
|
| 1129 |
|
| 1130 |
``` cpp
|
| 1131 |
auto glambda = [](int i, auto a) { return i; }; // OK: a generic lambda
|
| 1132 |
```
|
| 1133 |
|
|
|
|
|
|
|
| 1134 |
The type of a variable declared using `auto` or `decltype(auto)` is
|
| 1135 |
-
deduced from its initializer. This use is allowed
|
| 1136 |
-
|
| 1137 |
-
|
| 1138 |
-
|
| 1139 |
-
*
|
| 1140 |
-
one or more *init-declarator*s, each of which shall have a non-empty
|
| 1141 |
*initializer*. In an *initializer* of the form
|
| 1142 |
|
| 1143 |
``` cpp
|
| 1144 |
( expression-list )
|
| 1145 |
```
|
| 1146 |
|
| 1147 |
the *expression-list* shall be a single *assignment-expression*.
|
| 1148 |
|
|
|
|
|
|
|
| 1149 |
``` cpp
|
| 1150 |
auto x = 5; // OK: x has type int
|
| 1151 |
const auto *v = &x, u = 6; // OK: v has type const int*, u has type const int
|
| 1152 |
static auto y = 0.0; // OK: y has type double
|
| 1153 |
auto int r; // error: auto is not a storage-class-specifier
|
| 1154 |
auto f() -> int; // OK: f returns int
|
| 1155 |
auto g() { return 0.0; } // OK: g returns double
|
| 1156 |
auto h(); // OK: h's return type will be deduced when it is defined
|
| 1157 |
```
|
| 1158 |
|
| 1159 |
-
|
| 1160 |
-
|
| 1161 |
-
|
| 1162 |
-
*
|
| 1163 |
-
*
|
| 1164 |
-
|
| 1165 |
|
| 1166 |
A program that uses `auto` or `decltype(auto)` in a context not
|
| 1167 |
explicitly allowed in this section is ill-formed.
|
| 1168 |
|
| 1169 |
-
When a variable declared using a placeholder type is initialized, or a
|
| 1170 |
-
`return` statement occurs in a function declared with a return type that
|
| 1171 |
-
contains a placeholder type, the deduced return type or variable type is
|
| 1172 |
-
determined from the type of its initializer. In the case of a `return`
|
| 1173 |
-
with no operand, the initializer is considered to be `void()`. Let `T`
|
| 1174 |
-
be the declared type of the variable or return type of the function. If
|
| 1175 |
-
the placeholder is the `auto` *type-specifier*, the deduced type is
|
| 1176 |
-
determined using the rules for template argument deduction. If the
|
| 1177 |
-
deduction is for a `return` statement and the initializer is a
|
| 1178 |
-
*braced-init-list* ([[dcl.init.list]]), the program is ill-formed.
|
| 1179 |
-
Otherwise, obtain `P` from `T` by replacing the occurrences of `auto`
|
| 1180 |
-
with either a new invented type template parameter `U` or, if the
|
| 1181 |
-
initializer is a *braced-init-list*, with `std::initializer_list<U>`.
|
| 1182 |
-
Deduce a value for `U` using the rules of template argument deduction
|
| 1183 |
-
from a function call ([[temp.deduct.call]]), where `P` is a function
|
| 1184 |
-
template parameter type and the initializer is the corresponding
|
| 1185 |
-
argument. If the deduction fails, the declaration is ill-formed.
|
| 1186 |
-
Otherwise, the type deduced for the variable or return type is obtained
|
| 1187 |
-
by substituting the deduced `U` into `P`.
|
| 1188 |
-
|
| 1189 |
-
``` cpp
|
| 1190 |
-
auto x1 = { 1, 2 }; // decltype(x1) is std::initializer_list<int>
|
| 1191 |
-
auto x2 = { 1, 2.0 }; // error: cannot deduce element type
|
| 1192 |
-
```
|
| 1193 |
-
|
| 1194 |
-
``` cpp
|
| 1195 |
-
const auto &i = expr;
|
| 1196 |
-
```
|
| 1197 |
-
|
| 1198 |
-
The type of `i` is the deduced type of the parameter `u` in the call
|
| 1199 |
-
`f(expr)` of the following invented function template:
|
| 1200 |
-
|
| 1201 |
-
``` cpp
|
| 1202 |
-
template <class U> void f(const U& u);
|
| 1203 |
-
```
|
| 1204 |
-
|
| 1205 |
-
If the placeholder is the `decltype(auto)` *type-specifier*, the
|
| 1206 |
-
declared type of the variable or return type of the function shall be
|
| 1207 |
-
the placeholder alone. The type deduced for the variable or return type
|
| 1208 |
-
is determined as described in [[dcl.type.simple]], as though the
|
| 1209 |
-
initializer had been the operand of the `decltype`.
|
| 1210 |
-
|
| 1211 |
-
``` cpp
|
| 1212 |
-
int i;
|
| 1213 |
-
int&& f();
|
| 1214 |
-
auto x3a = i; // decltype(x3a) is int
|
| 1215 |
-
decltype(auto) x3d = i; // decltype(x3d) is int
|
| 1216 |
-
auto x4a = (i); // decltype(x4a) is int
|
| 1217 |
-
decltype(auto) x4d = (i); // decltype(x4d) is int&
|
| 1218 |
-
auto x5a = f(); // decltype(x5a) is int
|
| 1219 |
-
decltype(auto) x5d = f(); // decltype(x5d) is int&&
|
| 1220 |
-
auto x6a = { 1, 2 }; // decltype(x6a) is std::initializer_list<int>
|
| 1221 |
-
decltype(auto) x6d = { 1, 2 }; // error, { 1, 2 } is not an expression
|
| 1222 |
-
auto *x7a = &i; // decltype(x7a) is int*
|
| 1223 |
-
decltype(auto)*x7d = &i; // error, declared type is not plain decltype(auto)
|
| 1224 |
-
```
|
| 1225 |
-
|
| 1226 |
If the *init-declarator-list* contains more than one *init-declarator*,
|
| 1227 |
they shall all form declarations of variables. The type of each declared
|
| 1228 |
-
variable is determined
|
| 1229 |
-
|
| 1230 |
-
ill-formed.
|
|
|
|
|
|
|
| 1231 |
|
| 1232 |
``` cpp
|
| 1233 |
auto x = 5, *y = &x; // OK: auto is int
|
| 1234 |
auto a = 5, b = { 1, 2 }; // error: different types for auto
|
| 1235 |
```
|
| 1236 |
|
|
|
|
|
|
|
| 1237 |
If a function with a declared return type that contains a placeholder
|
| 1238 |
-
type has multiple `return` statements, the return type is
|
| 1239 |
-
each `return` statement. If the type deduced is not the
|
| 1240 |
-
deduction, the program is ill-formed.
|
| 1241 |
|
| 1242 |
If a function with a declared return type that uses a placeholder type
|
| 1243 |
-
has no `return` statements, the return type is deduced as
|
| 1244 |
-
`return` statement with no operand at the closing brace of
|
| 1245 |
-
body.
|
|
|
|
|
|
|
| 1246 |
|
| 1247 |
``` cpp
|
| 1248 |
auto f() { } // OK, return type is void
|
| 1249 |
auto* g() { } // error, cannot deduce auto* from void()
|
| 1250 |
```
|
| 1251 |
|
|
|
|
|
|
|
| 1252 |
If the type of an entity with an undeduced placeholder type is needed to
|
| 1253 |
determine the type of an expression, the program is ill-formed. Once a
|
| 1254 |
-
`return` statement has been seen in a function, however,
|
| 1255 |
-
deduced from that statement can be used in the rest of
|
| 1256 |
-
including in other `return` statements.
|
|
|
|
|
|
|
| 1257 |
|
| 1258 |
``` cpp
|
| 1259 |
auto n = n; // error, n's type is unknown
|
| 1260 |
auto f();
|
| 1261 |
void g() { &f; } // error, f's return type is unknown
|
|
@@ -1265,30 +1416,41 @@ auto sum(int i) {
|
|
| 1265 |
else
|
| 1266 |
return sum(i-1)+i; // OK, sum's return type has been deduced
|
| 1267 |
}
|
| 1268 |
```
|
| 1269 |
|
|
|
|
|
|
|
| 1270 |
Return type deduction for a function template with a placeholder in its
|
| 1271 |
declared type occurs when the definition is instantiated even if the
|
| 1272 |
function body contains a `return` statement with a non-type-dependent
|
| 1273 |
-
operand.
|
| 1274 |
-
|
| 1275 |
-
|
| 1276 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1277 |
|
| 1278 |
``` cpp
|
| 1279 |
template <class T> auto f(T t) { return t; } // return type deduced at instantiation time
|
| 1280 |
typedef decltype(f(1)) fint_t; // instantiates f<int> to deduce return type
|
| 1281 |
template<class T> auto f(T* t) { return *t; }
|
| 1282 |
void g() { int (*p)(int*) = &f; } // instantiates both fs to determine return types,
|
| 1283 |
// chooses second
|
| 1284 |
```
|
| 1285 |
|
|
|
|
|
|
|
| 1286 |
Redeclarations or specializations of a function or function template
|
| 1287 |
with a declared return type that uses a placeholder type shall also use
|
| 1288 |
that placeholder, not a deduced type.
|
| 1289 |
|
|
|
|
|
|
|
| 1290 |
``` cpp
|
| 1291 |
auto f();
|
| 1292 |
auto f() { return 42; } // return type is int
|
| 1293 |
auto f(); // OK
|
| 1294 |
int f(); // error, cannot be overloaded with auto f()
|
|
@@ -1309,29 +1471,164 @@ template <typename T> struct A {
|
|
| 1309 |
friend T frf(T);
|
| 1310 |
};
|
| 1311 |
auto frf(int i) { return i; } // not a friend of A<int>
|
| 1312 |
```
|
| 1313 |
|
|
|
|
|
|
|
| 1314 |
A function declared with a return type that uses a placeholder type
|
| 1315 |
shall not be `virtual` ([[class.virtual]]).
|
| 1316 |
|
| 1317 |
An explicit instantiation declaration ([[temp.explicit]]) does not
|
| 1318 |
cause the instantiation of an entity declared using a placeholder type,
|
| 1319 |
but it also does not prevent that entity from being instantiated as
|
| 1320 |
needed to determine its type.
|
| 1321 |
|
|
|
|
|
|
|
| 1322 |
``` cpp
|
| 1323 |
template <typename T> auto f(T t) { return t; }
|
| 1324 |
extern template auto f(int); // does not instantiate f<int>
|
| 1325 |
int (*p)(int) = f; // instantiates f<int> to determine its return type, but an explicit
|
| 1326 |
// instantiation definition is still required somewhere in the program
|
| 1327 |
```
|
| 1328 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1329 |
## Enumeration declarations <a id="dcl.enum">[[dcl.enum]]</a>
|
| 1330 |
|
| 1331 |
An enumeration is a distinct type ([[basic.compound]]) with named
|
| 1332 |
-
constants. Its name becomes an *enum-name*
|
| 1333 |
|
| 1334 |
``` bnf
|
| 1335 |
enum-name:
|
| 1336 |
identifier
|
| 1337 |
```
|
|
@@ -1342,18 +1639,21 @@ enum-specifier:
|
|
| 1342 |
enum-head '{' enumerator-list ', }'
|
| 1343 |
```
|
| 1344 |
|
| 1345 |
``` bnf
|
| 1346 |
enum-head:
|
| 1347 |
-
enum-key attribute-specifier-seqₒₚₜ
|
| 1348 |
-
|
| 1349 |
-
|
|
|
|
|
|
|
|
|
|
| 1350 |
```
|
| 1351 |
|
| 1352 |
``` bnf
|
| 1353 |
opaque-enum-declaration:
|
| 1354 |
-
enum-key attribute-specifier-seqₒₚₜ identifier enum-baseₒₚₜ ';'
|
| 1355 |
```
|
| 1356 |
|
| 1357 |
``` bnf
|
| 1358 |
enum-key:
|
| 1359 |
'enum'
|
|
@@ -1378,61 +1678,85 @@ enumerator-definition:
|
|
| 1378 |
enumerator '=' constant-expression
|
| 1379 |
```
|
| 1380 |
|
| 1381 |
``` bnf
|
| 1382 |
enumerator:
|
| 1383 |
-
identifier
|
| 1384 |
```
|
| 1385 |
|
| 1386 |
The optional *attribute-specifier-seq* in the *enum-head* and the
|
| 1387 |
*opaque-enum-declaration* appertains to the enumeration; the attributes
|
| 1388 |
in that *attribute-specifier-seq* are thereafter considered attributes
|
| 1389 |
of the enumeration whenever it is named. A `:` following “`enum`
|
| 1390 |
-
*identifier*”
|
| 1391 |
-
|
| 1392 |
-
*enum-base*
|
| 1393 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1394 |
|
| 1395 |
``` cpp
|
| 1396 |
struct S {
|
| 1397 |
enum E : int {};
|
| 1398 |
enum E : int {}; // error: redeclaration of enumeration
|
| 1399 |
};
|
| 1400 |
```
|
| 1401 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1402 |
The enumeration type declared with an *enum-key* of only `enum` is an
|
| 1403 |
-
unscoped enumeration, and its *enumerator*s are *unscoped
|
| 1404 |
-
The *enum-key*s `enum class` and `enum struct` are
|
| 1405 |
-
equivalent; an enumeration type declared with one of these
|
| 1406 |
-
enumeration*, and its *enumerator*s are *scoped
|
| 1407 |
-
optional *identifier* shall not be omitted in the
|
| 1408 |
-
scoped enumeration. The *type-specifier-seq* of an
|
| 1409 |
-
name an integral type; any cv-qualification is
|
| 1410 |
-
*opaque-enum-declaration* declaring an unscoped enumeration
|
| 1411 |
-
omit the *enum-base*. The identifiers in an *enumerator-list*
|
| 1412 |
-
declared as constants, and can appear wherever constants are
|
| 1413 |
-
An *enumerator-definition* with `=` gives the associated
|
| 1414 |
-
the value indicated by the *constant-expression*. If the
|
| 1415 |
-
*enumerator* has no *initializer*, the value of the corresponding
|
| 1416 |
constant is zero. An *enumerator-definition* without an *initializer*
|
| 1417 |
gives the *enumerator* the value obtained by increasing the value of the
|
| 1418 |
previous *enumerator* by one.
|
| 1419 |
|
|
|
|
|
|
|
| 1420 |
``` cpp
|
| 1421 |
enum { a, b, c=0 };
|
| 1422 |
enum { d, e, f=e+2 };
|
| 1423 |
```
|
| 1424 |
|
| 1425 |
defines `a`, `c`, and `d` to be zero, `b` and `e` to be `1`, and `f` to
|
| 1426 |
be `3`.
|
| 1427 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1428 |
An *opaque-enum-declaration* is either a redeclaration of an enumeration
|
| 1429 |
-
in the current scope or a declaration of a new enumeration.
|
| 1430 |
-
|
| 1431 |
-
|
| 1432 |
-
|
| 1433 |
-
|
|
|
|
|
|
|
|
|
|
| 1434 |
different underlying type. An unscoped enumeration shall not be later
|
| 1435 |
redeclared as scoped and each redeclaration shall include an *enum-base*
|
| 1436 |
specifying the same underlying type as in the original declaration.
|
| 1437 |
|
| 1438 |
If the *enum-key* is followed by a *nested-name-specifier*, the
|
|
@@ -1441,12 +1765,12 @@ declared directly in the class or namespace to which the
|
|
| 1441 |
*nested-name-specifier* refers (i.e., neither inherited nor introduced
|
| 1442 |
by a *using-declaration*), and the *enum-specifier* shall appear in a
|
| 1443 |
namespace enclosing the previous declaration.
|
| 1444 |
|
| 1445 |
Each enumeration defines a type that is different from all other types.
|
| 1446 |
-
Each enumeration also has an underlying type. The underlying type can
|
| 1447 |
-
explicitly specified using an *enum-base*. For a scoped enumeration
|
| 1448 |
type, the underlying type is `int` if it is not explicitly specified. In
|
| 1449 |
both of these cases, the underlying type is said to be *fixed*.
|
| 1450 |
Following the closing brace of an *enum-specifier*, each enumerator has
|
| 1451 |
the type of its enumeration. If the underlying type is fixed, the type
|
| 1452 |
of each enumerator prior to the closing brace is the underlying type and
|
|
@@ -1486,29 +1810,31 @@ unless the value of an enumerator cannot fit in an `int` or
|
|
| 1486 |
is as if the enumeration had a single enumerator with value 0.
|
| 1487 |
|
| 1488 |
For an enumeration whose underlying type is fixed, the values of the
|
| 1489 |
enumeration are the values of the underlying type. Otherwise, for an
|
| 1490 |
enumeration where eₘin is the smallest enumerator and eₘax is the
|
| 1491 |
-
largest, the values of the enumeration are the values in the range bₘ
|
| 1492 |
-
to bₘ
|
| 1493 |
-
representation and 0 for a
|
| 1494 |
-
representation. bₘ
|
| 1495 |
-
max(|eₘ
|
| 1496 |
-
integer. bₘ
|
| 1497 |
The size of the smallest bit-field large enough to hold all the values
|
| 1498 |
-
of the enumeration type is max(M,1) if bₘ
|
| 1499 |
It is possible to define an enumeration that has values not defined by
|
| 1500 |
any of its enumerators. If the *enumerator-list* is empty, the values of
|
| 1501 |
the enumeration are as if the enumeration had a single enumerator with
|
| 1502 |
value 0.[^4]
|
| 1503 |
|
| 1504 |
-
Two enumeration types are *layout-compatible* if they have
|
| 1505 |
-
|
| 1506 |
|
| 1507 |
The value of an enumerator or an object of an unscoped enumeration type
|
| 1508 |
is converted to an integer by integral promotion ([[conv.prom]]).
|
| 1509 |
|
|
|
|
|
|
|
| 1510 |
``` cpp
|
| 1511 |
enum color { red, yellow, green=20, blue };
|
| 1512 |
color col = red;
|
| 1513 |
color* cp = &col;
|
| 1514 |
if (*cp == blue) // ...
|
|
@@ -1520,15 +1846,12 @@ type. The possible values of an object of type `color` are `red`,
|
|
| 1520 |
`yellow`, `green`, `blue`; these values can be converted to the integral
|
| 1521 |
values `0`, `1`, `20`, and `21`. Since enumerations are distinct types,
|
| 1522 |
objects of type `color` can be assigned only values of type `color`.
|
| 1523 |
|
| 1524 |
``` cpp
|
| 1525 |
-
color c = 1; // error: type mismatch,
|
| 1526 |
-
|
| 1527 |
-
|
| 1528 |
-
int i = yellow; // OK: yellow converted to integral value 1
|
| 1529 |
-
// integral promotion
|
| 1530 |
```
|
| 1531 |
|
| 1532 |
Note that this implicit `enum` to `int` conversion is not provided for a
|
| 1533 |
scoped enumeration:
|
| 1534 |
|
|
@@ -1537,15 +1860,18 @@ enum class Col { red, yellow, green };
|
|
| 1537 |
int x = Col::red; // error: no Col to int conversion
|
| 1538 |
Col y = Col::red;
|
| 1539 |
if (y) { } // error: no Col to bool conversion
|
| 1540 |
```
|
| 1541 |
|
|
|
|
|
|
|
| 1542 |
Each *enum-name* and each unscoped *enumerator* is declared in the scope
|
| 1543 |
that immediately contains the *enum-specifier*. Each scoped *enumerator*
|
| 1544 |
is declared in the scope of the enumeration. These names obey the scope
|
| 1545 |
-
rules defined for all names in
|
| 1546 |
-
|
|
|
|
| 1547 |
|
| 1548 |
``` cpp
|
| 1549 |
enum direction { left='l', right='r' };
|
| 1550 |
|
| 1551 |
void g() {
|
|
@@ -1561,14 +1887,18 @@ void h() {
|
|
| 1561 |
a = high; // error: high not in scope
|
| 1562 |
a = altitude::low; // OK
|
| 1563 |
}
|
| 1564 |
```
|
| 1565 |
|
|
|
|
|
|
|
| 1566 |
An enumerator declared in class scope can be referred to using the class
|
| 1567 |
member access operators (`::`, `.` (dot) and `->` (arrow)), see
|
| 1568 |
[[expr.ref]].
|
| 1569 |
|
|
|
|
|
|
|
| 1570 |
``` cpp
|
| 1571 |
struct X {
|
| 1572 |
enum direction { left='l', right='r' };
|
| 1573 |
int f(int i) { return i==left ? 0 : i==right ? 1 : 2; }
|
| 1574 |
};
|
|
@@ -1581,10 +1911,22 @@ void g(X* p) {
|
|
| 1581 |
i = p->f(p->left); // OK
|
| 1582 |
// ...
|
| 1583 |
}
|
| 1584 |
```
|
| 1585 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1586 |
## Namespaces <a id="basic.namespace">[[basic.namespace]]</a>
|
| 1587 |
|
| 1588 |
A namespace is an optionally-named declarative region. The name of a
|
| 1589 |
namespace can be used to access entities declared in that namespace;
|
| 1590 |
that is, the members of the namespace. Unlike other declarative regions,
|
|
@@ -1594,72 +1936,67 @@ more translation units.
|
|
| 1594 |
The outermost declarative region of a translation unit is a namespace;
|
| 1595 |
see [[basic.scope.namespace]].
|
| 1596 |
|
| 1597 |
### Namespace definition <a id="namespace.def">[[namespace.def]]</a>
|
| 1598 |
|
| 1599 |
-
The grammar for a *namespace-definition* is
|
| 1600 |
-
|
| 1601 |
``` bnf
|
| 1602 |
namespace-name:
|
| 1603 |
-
|
| 1604 |
namespace-alias
|
| 1605 |
```
|
| 1606 |
|
| 1607 |
-
``` bnf
|
| 1608 |
-
original-namespace-name:
|
| 1609 |
-
identifier
|
| 1610 |
-
```
|
| 1611 |
-
|
| 1612 |
``` bnf
|
| 1613 |
namespace-definition:
|
| 1614 |
named-namespace-definition
|
| 1615 |
unnamed-namespace-definition
|
|
|
|
| 1616 |
```
|
| 1617 |
|
| 1618 |
``` bnf
|
| 1619 |
named-namespace-definition:
|
| 1620 |
-
|
| 1621 |
-
extension-namespace-definition
|
| 1622 |
-
```
|
| 1623 |
-
|
| 1624 |
-
``` bnf
|
| 1625 |
-
original-namespace-definition:
|
| 1626 |
-
'inline'ₒₚₜ 'namespace' identifier '{' namespace-body '}'
|
| 1627 |
-
```
|
| 1628 |
-
|
| 1629 |
-
``` bnf
|
| 1630 |
-
extension-namespace-definition:
|
| 1631 |
-
'inline'ₒₚₜ 'namespace' original-namespace-name '{' namespace-body '}'
|
| 1632 |
```
|
| 1633 |
|
| 1634 |
``` bnf
|
| 1635 |
unnamed-namespace-definition:
|
| 1636 |
-
'inline'ₒₚₜ 'namespace {' namespace-body '}'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1637 |
```
|
| 1638 |
|
| 1639 |
``` bnf
|
| 1640 |
namespace-body:
|
| 1641 |
declaration-seqₒₚₜ
|
| 1642 |
```
|
| 1643 |
|
| 1644 |
-
The *identifier* in an *original-namespace-definition* shall not have
|
| 1645 |
-
been previously defined in the declarative region in which the
|
| 1646 |
-
*original-namespace-definition* appears. The *identifier* in an
|
| 1647 |
-
*original-namespace-definition* is the name of the namespace.
|
| 1648 |
-
Subsequently in that declarative region, it is treated as an
|
| 1649 |
-
*original-namespace-name*.
|
| 1650 |
-
|
| 1651 |
-
The *original-namespace-name* in an *extension-namespace-definition*
|
| 1652 |
-
shall have previously been defined in an *original-namespace-definition*
|
| 1653 |
-
in the same declarative region.
|
| 1654 |
-
|
| 1655 |
Every *namespace-definition* shall appear in the global scope or in a
|
| 1656 |
namespace scope ([[basic.scope.namespace]]).
|
| 1657 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1658 |
Because a *namespace-definition* contains *declaration*s in its
|
| 1659 |
*namespace-body* and a *namespace-definition* is itself a *declaration*,
|
| 1660 |
-
it follows that *namespace-
|
|
|
|
|
|
|
| 1661 |
|
| 1662 |
``` cpp
|
| 1663 |
namespace Outer {
|
| 1664 |
int i;
|
| 1665 |
namespace Inner {
|
|
@@ -1668,16 +2005,20 @@ namespace Outer {
|
|
| 1668 |
void g() { i++; } // Inner::i
|
| 1669 |
}
|
| 1670 |
}
|
| 1671 |
```
|
| 1672 |
|
|
|
|
|
|
|
| 1673 |
The *enclosing namespaces* of a declaration are those namespaces in
|
| 1674 |
which the declaration lexically appears, except for a redeclaration of a
|
| 1675 |
namespace member outside its original namespace (e.g., a definition as
|
| 1676 |
specified in [[namespace.memdef]]). Such a redeclaration has the same
|
| 1677 |
enclosing namespaces as the original declaration.
|
| 1678 |
|
|
|
|
|
|
|
| 1679 |
``` cpp
|
| 1680 |
namespace Q {
|
| 1681 |
namespace V {
|
| 1682 |
void f(); // enclosing namespaces are the global namespace, Q, and Q::V
|
| 1683 |
class C { void m(); };
|
|
@@ -1688,55 +2029,94 @@ namespace Q {
|
|
| 1688 |
void V::C::m() { // enclosing namespaces are the global namespace, Q, and Q::V
|
| 1689 |
}
|
| 1690 |
}
|
| 1691 |
```
|
| 1692 |
|
|
|
|
|
|
|
| 1693 |
If the optional initial `inline` keyword appears in a
|
| 1694 |
*namespace-definition* for a particular namespace, that namespace is
|
| 1695 |
declared to be an *inline namespace*. The `inline` keyword may be used
|
| 1696 |
-
on
|
| 1697 |
-
the *
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1698 |
|
| 1699 |
Members of an inline namespace can be used in most respects as though
|
| 1700 |
they were members of the enclosing namespace. Specifically, the inline
|
| 1701 |
namespace and its enclosing namespace are both added to the set of
|
| 1702 |
associated namespaces used in argument-dependent lookup (
|
| 1703 |
[[basic.lookup.argdep]]) whenever one of them is, and a
|
| 1704 |
*using-directive* ([[namespace.udir]]) that names the inline namespace
|
| 1705 |
is implicitly inserted into the enclosing namespace as for an unnamed
|
| 1706 |
namespace ([[namespace.unnamed]]). Furthermore, each member of the
|
| 1707 |
-
inline namespace can subsequently be
|
| 1708 |
-
[[temp.
|
| 1709 |
-
though it were a member
|
| 1710 |
-
|
| 1711 |
-
[[namespace.qual]]) will include
|
| 1712 |
-
in by the *using-directive* even
|
| 1713 |
-
in the enclosing namespace.
|
| 1714 |
|
| 1715 |
These properties are transitive: if a namespace `N` contains an inline
|
| 1716 |
namespace `M`, which in turn contains an inline namespace `O`, then the
|
| 1717 |
members of `O` can be used as though they were members of `M` or `N`.
|
| 1718 |
The *inline namespace set* of `N` is the transitive closure of all
|
| 1719 |
inline namespaces in `N`. The *enclosing namespace set* of `O` is the
|
| 1720 |
set of namespaces consisting of the innermost non-inline namespace
|
| 1721 |
enclosing an inline namespace `O`, together with any intervening inline
|
| 1722 |
namespaces.
|
| 1723 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1724 |
#### Unnamed namespaces <a id="namespace.unnamed">[[namespace.unnamed]]</a>
|
| 1725 |
|
| 1726 |
An *unnamed-namespace-definition* behaves as if it were replaced by
|
| 1727 |
|
| 1728 |
``` bnf
|
| 1729 |
-
'inline'ₒₚₜ 'namespace' unique '{ /* empty body */ }'
|
| 1730 |
-
'using namespace' unique ';'
|
| 1731 |
-
'namespace' unique '{' namespace-body '}'
|
| 1732 |
```
|
| 1733 |
|
| 1734 |
where `inline` appears if and only if it appears in the
|
| 1735 |
-
*unnamed-namespace-definition*
|
| 1736 |
translation unit are replaced by the same identifier, and this
|
| 1737 |
-
identifier differs from all other identifiers in the
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1738 |
|
| 1739 |
``` cpp
|
| 1740 |
namespace { int i; } // unique ::i
|
| 1741 |
void f() { i++; } // unique ::i++
|
| 1742 |
|
|
@@ -1754,64 +2134,95 @@ void h() {
|
|
| 1754 |
A::i++; // A::unique ::i
|
| 1755 |
j++; // A::unique ::j
|
| 1756 |
}
|
| 1757 |
```
|
| 1758 |
|
|
|
|
|
|
|
| 1759 |
#### Namespace member definitions <a id="namespace.memdef">[[namespace.memdef]]</a>
|
| 1760 |
|
| 1761 |
-
|
| 1762 |
-
|
| 1763 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1764 |
|
| 1765 |
``` cpp
|
| 1766 |
namespace X {
|
| 1767 |
-
void f() {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1768 |
}
|
| 1769 |
```
|
| 1770 |
|
|
|
|
|
|
|
| 1771 |
Members of a named namespace can also be defined outside that namespace
|
| 1772 |
by explicit qualification ([[namespace.qual]]) of the name being
|
| 1773 |
defined, provided that the entity being defined was already declared in
|
| 1774 |
the namespace and the definition appears after the point of declaration
|
| 1775 |
in a namespace that encloses the declaration’s namespace.
|
| 1776 |
|
|
|
|
|
|
|
| 1777 |
``` cpp
|
| 1778 |
namespace Q {
|
| 1779 |
namespace V {
|
| 1780 |
void f();
|
| 1781 |
}
|
| 1782 |
-
void V::f() {
|
| 1783 |
-
void V::g() {
|
| 1784 |
namespace V {
|
| 1785 |
void g();
|
| 1786 |
}
|
| 1787 |
}
|
| 1788 |
|
| 1789 |
namespace R {
|
| 1790 |
-
void Q::V::g() {
|
| 1791 |
}
|
| 1792 |
```
|
| 1793 |
|
| 1794 |
-
|
|
|
|
| 1795 |
If a `friend` declaration in a non-local class first declares a class,
|
| 1796 |
-
function, class template or function template[^
|
| 1797 |
of the innermost enclosing namespace. The `friend` declaration does not
|
| 1798 |
by itself make the name visible to unqualified lookup (
|
| 1799 |
[[basic.lookup.unqual]]) or qualified lookup ([[basic.lookup.qual]]).
|
| 1800 |
-
|
| 1801 |
-
|
| 1802 |
-
|
| 1803 |
-
|
| 1804 |
-
|
| 1805 |
-
|
| 1806 |
-
|
| 1807 |
-
|
| 1808 |
-
lookup
|
| 1809 |
-
|
| 1810 |
-
|
| 1811 |
-
|
| 1812 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1813 |
|
| 1814 |
``` cpp
|
| 1815 |
// Assume f and g have not yet been declared.
|
| 1816 |
void h(int);
|
| 1817 |
template <class T> void f2(T);
|
|
@@ -1827,12 +2238,12 @@ namespace A {
|
|
| 1827 |
};
|
| 1828 |
|
| 1829 |
// A::f, A::g and A::h are not visible here
|
| 1830 |
X x;
|
| 1831 |
void g() { f(x); } // definition of A::g
|
| 1832 |
-
void f(X) {
|
| 1833 |
-
void h(int) {
|
| 1834 |
// A::f, A::g and A::h are visible here and known to be friends
|
| 1835 |
}
|
| 1836 |
|
| 1837 |
using A::x;
|
| 1838 |
|
|
@@ -1841,10 +2252,12 @@ void h() {
|
|
| 1841 |
A::X::f(x); // error: f is not a member of A::X
|
| 1842 |
A::X::Y::g(); // error: g is not a member of A::X::Y
|
| 1843 |
}
|
| 1844 |
```
|
| 1845 |
|
|
|
|
|
|
|
| 1846 |
### Namespace alias <a id="namespace.alias">[[namespace.alias]]</a>
|
| 1847 |
|
| 1848 |
A *namespace-alias-definition* declares an alternate name for a
|
| 1849 |
namespace according to the following grammar:
|
| 1850 |
|
|
@@ -1863,57 +2276,75 @@ qualified-namespace-specifier:
|
|
| 1863 |
nested-name-specifierₒₚₜ namespace-name
|
| 1864 |
```
|
| 1865 |
|
| 1866 |
The *identifier* in a *namespace-alias-definition* is a synonym for the
|
| 1867 |
name of the namespace denoted by the *qualified-namespace-specifier* and
|
| 1868 |
-
becomes a *namespace-alias*.
|
|
|
|
|
|
|
| 1869 |
*namespace-alias-definition*, only namespace names are considered, see
|
| 1870 |
-
[[basic.lookup.udir]].
|
| 1871 |
|
| 1872 |
In a declarative region, a *namespace-alias-definition* can be used to
|
| 1873 |
redefine a *namespace-alias* declared in that declarative region to
|
| 1874 |
-
refer only to the namespace to which it already refers.
|
| 1875 |
-
|
|
|
|
|
|
|
|
|
|
| 1876 |
|
| 1877 |
``` cpp
|
| 1878 |
-
namespace Company_with_very_long_name {
|
| 1879 |
namespace CWVLN = Company_with_very_long_name;
|
| 1880 |
namespace CWVLN = Company_with_very_long_name; // OK: duplicate
|
| 1881 |
namespace CWVLN = CWVLN;
|
| 1882 |
```
|
| 1883 |
|
| 1884 |
-
|
| 1885 |
-
name of any other entity in the same declarative region. A
|
| 1886 |
-
*namespace-name* defined at global scope shall not be declared as the
|
| 1887 |
-
name of any other entity in any global scope of the program. No
|
| 1888 |
-
diagnostic is required for a violation of this rule by declarations in
|
| 1889 |
-
different translation units.
|
| 1890 |
|
| 1891 |
### The `using` declaration <a id="namespace.udecl">[[namespace.udecl]]</a>
|
| 1892 |
|
| 1893 |
-
A *using-declaration* introduces a name into the declarative region in
|
| 1894 |
-
which the *using-declaration* appears.
|
| 1895 |
-
|
| 1896 |
``` bnf
|
| 1897 |
using-declaration:
|
| 1898 |
-
'using
|
| 1899 |
-
'using ::' unqualified-id ';'
|
| 1900 |
```
|
| 1901 |
|
| 1902 |
-
|
| 1903 |
-
|
| 1904 |
-
|
| 1905 |
-
|
| 1906 |
-
|
| 1907 |
-
|
| 1908 |
-
|
| 1909 |
-
|
| 1910 |
-
|
| 1911 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1912 |
|
| 1913 |
Every *using-declaration* is a *declaration* and a *member-declaration*
|
| 1914 |
-
and
|
|
|
|
|
|
|
| 1915 |
|
| 1916 |
``` cpp
|
| 1917 |
struct B {
|
| 1918 |
void f(char);
|
| 1919 |
void g(char);
|
|
@@ -1926,16 +2357,32 @@ struct D : B {
|
|
| 1926 |
void f(int) { f('c'); } // calls B::f(char)
|
| 1927 |
void g(int) { g('c'); } // recursively calls D::g(int)
|
| 1928 |
};
|
| 1929 |
```
|
| 1930 |
|
| 1931 |
-
|
| 1932 |
-
|
| 1933 |
-
|
| 1934 |
-
*nested-name-specifier* shall name a
|
| 1935 |
-
being defined
|
| 1936 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1937 |
|
| 1938 |
``` cpp
|
| 1939 |
class C {
|
| 1940 |
int g();
|
| 1941 |
};
|
|
@@ -1946,25 +2393,30 @@ class D2 : public B {
|
|
| 1946 |
using B::x; // OK: x is a union member of base B
|
| 1947 |
using C::g; // error: C isn't a base of D2
|
| 1948 |
};
|
| 1949 |
```
|
| 1950 |
|
| 1951 |
-
|
| 1952 |
-
|
| 1953 |
-
|
| 1954 |
-
|
| 1955 |
-
|
| 1956 |
-
|
| 1957 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1958 |
*using-declaration* does not by itself suppress the implicit declaration
|
| 1959 |
-
of the derived class
|
| 1960 |
-
|
| 1961 |
-
|
| 1962 |
-
as described below.
|
| 1963 |
|
| 1964 |
A *using-declaration* shall not name a *template-id*.
|
| 1965 |
|
|
|
|
|
|
|
| 1966 |
``` cpp
|
| 1967 |
struct A {
|
| 1968 |
template <class T> void f(T);
|
| 1969 |
template <class T> struct X { };
|
| 1970 |
};
|
|
@@ -1972,34 +2424,39 @@ struct B : A {
|
|
| 1972 |
using A::f<double>; // ill-formed
|
| 1973 |
using A::X<int>; // ill-formed
|
| 1974 |
};
|
| 1975 |
```
|
| 1976 |
|
|
|
|
|
|
|
| 1977 |
A *using-declaration* shall not name a namespace.
|
| 1978 |
|
| 1979 |
A *using-declaration* shall not name a scoped enumerator.
|
| 1980 |
|
| 1981 |
-
A *using-declaration*
|
| 1982 |
*member-declaration*.
|
| 1983 |
|
|
|
|
|
|
|
| 1984 |
``` cpp
|
| 1985 |
struct X {
|
| 1986 |
int i;
|
| 1987 |
static int s;
|
| 1988 |
};
|
| 1989 |
|
| 1990 |
void f() {
|
| 1991 |
-
using X::i;
|
| 1992 |
-
|
| 1993 |
-
using X::s; // error: X::s is a class member
|
| 1994 |
-
// and this is not a member declaration.
|
| 1995 |
}
|
| 1996 |
```
|
| 1997 |
|
|
|
|
|
|
|
| 1998 |
Members declared by a *using-declaration* can be referred to by explicit
|
| 1999 |
-
qualification just like other member names ([[namespace.qual]]).
|
| 2000 |
-
|
|
|
|
| 2001 |
|
| 2002 |
``` cpp
|
| 2003 |
void f();
|
| 2004 |
|
| 2005 |
namespace A {
|
|
@@ -2016,78 +2473,83 @@ void h()
|
|
| 2016 |
X::f(); // calls ::f
|
| 2017 |
X::g(); // calls A::g
|
| 2018 |
}
|
| 2019 |
```
|
| 2020 |
|
|
|
|
|
|
|
| 2021 |
A *using-declaration* is a *declaration* and can therefore be used
|
| 2022 |
repeatedly where (and only where) multiple declarations are allowed.
|
| 2023 |
|
|
|
|
|
|
|
| 2024 |
``` cpp
|
| 2025 |
namespace A {
|
| 2026 |
int i;
|
| 2027 |
}
|
| 2028 |
|
| 2029 |
namespace A1 {
|
| 2030 |
-
using A::i;
|
| 2031 |
-
using A::i; // OK: double declaration
|
| 2032 |
-
}
|
| 2033 |
-
|
| 2034 |
-
void f() {
|
| 2035 |
-
using A::i;
|
| 2036 |
-
using A::i; // error: double declaration
|
| 2037 |
}
|
| 2038 |
|
| 2039 |
struct B {
|
| 2040 |
int i;
|
| 2041 |
};
|
| 2042 |
|
| 2043 |
struct X : B {
|
| 2044 |
-
using B::i;
|
| 2045 |
-
using B::i; // error: double member declaration
|
| 2046 |
};
|
| 2047 |
```
|
| 2048 |
|
| 2049 |
-
|
| 2050 |
-
|
| 2051 |
-
|
| 2052 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2053 |
[[temp.param]]), and template specializations ([[temp.class.spec]],
|
| 2054 |
-
[[temp.expl.spec]]) are considered.
|
|
|
|
|
|
|
| 2055 |
|
| 2056 |
``` cpp
|
| 2057 |
namespace A {
|
| 2058 |
void f(int);
|
| 2059 |
}
|
| 2060 |
|
| 2061 |
-
using A::f; // f is a synonym for A::f;
|
| 2062 |
-
// that is, for A::f(int).
|
| 2063 |
namespace A {
|
| 2064 |
void f(char);
|
| 2065 |
}
|
| 2066 |
|
| 2067 |
void foo() {
|
| 2068 |
-
f('a'); // calls f(int),
|
| 2069 |
-
}
|
| 2070 |
|
| 2071 |
void bar() {
|
| 2072 |
-
using A::f; // f is a synonym for A::f;
|
| 2073 |
-
// that is, for A::f(int) and A::f(char).
|
| 2074 |
f('a'); // calls f(char)
|
| 2075 |
}
|
| 2076 |
```
|
| 2077 |
|
| 2078 |
-
|
| 2079 |
-
|
| 2080 |
-
|
| 2081 |
-
|
| 2082 |
-
|
| 2083 |
-
|
|
|
|
|
|
|
| 2084 |
|
| 2085 |
Since a *using-declaration* is a declaration, the restrictions on
|
| 2086 |
declarations of the same name in the same declarative region (
|
| 2087 |
[[basic.scope]]) also apply to *using-declaration*s.
|
| 2088 |
|
|
|
|
|
|
|
| 2089 |
``` cpp
|
| 2090 |
namespace A {
|
| 2091 |
int x;
|
| 2092 |
}
|
| 2093 |
|
|
@@ -2114,22 +2576,29 @@ void func() {
|
|
| 2114 |
x = 99; // assigns to A::x
|
| 2115 |
struct x x1; // x1 has class type B::x
|
| 2116 |
}
|
| 2117 |
```
|
| 2118 |
|
|
|
|
|
|
|
| 2119 |
If a function declaration in namespace scope or block scope has the same
|
| 2120 |
name and the same parameter-type-list ([[dcl.fct]]) as a function
|
| 2121 |
introduced by a *using-declaration*, and the declarations do not declare
|
| 2122 |
the same function, the program is ill-formed. If a function template
|
| 2123 |
declaration in namespace scope has the same name, parameter-type-list,
|
| 2124 |
return type, and template parameter list as a function template
|
| 2125 |
-
introduced by a *using-declaration*, the program is ill-formed.
|
| 2126 |
-
|
| 2127 |
-
|
|
|
|
|
|
|
|
|
|
| 2128 |
name, function overload resolution selects the functions introduced by
|
| 2129 |
such *using-declaration*s, the function call is ill-formed.
|
| 2130 |
|
|
|
|
|
|
|
| 2131 |
``` cpp
|
| 2132 |
namespace B {
|
| 2133 |
void f(int);
|
| 2134 |
void f(double);
|
| 2135 |
}
|
|
@@ -2146,17 +2615,23 @@ void h() {
|
|
| 2146 |
f(1); // error: ambiguous: B::f(int) or C::f(int)?
|
| 2147 |
void f(int); // error: f(int) conflicts with C::f(int) and B::f(int)
|
| 2148 |
}
|
| 2149 |
```
|
| 2150 |
|
| 2151 |
-
|
| 2152 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2153 |
derived class override and/or hide member functions and member function
|
| 2154 |
templates with the same name, parameter-type-list ([[dcl.fct]]),
|
| 2155 |
cv-qualification, and *ref-qualifier* (if any) in a base class (rather
|
| 2156 |
-
than conflicting).
|
| 2157 |
-
|
|
|
|
|
|
|
| 2158 |
|
| 2159 |
``` cpp
|
| 2160 |
struct B {
|
| 2161 |
virtual void f(int);
|
| 2162 |
virtual void f(char);
|
|
@@ -2180,32 +2655,70 @@ void k(D* p)
|
|
| 2180 |
p->f(1); // calls D::f(int)
|
| 2181 |
p->f('a'); // calls B::f(char)
|
| 2182 |
p->g(1); // calls B::g(int)
|
| 2183 |
p->g('a'); // calls D::g(char)
|
| 2184 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2185 |
```
|
| 2186 |
|
| 2187 |
-
|
| 2188 |
-
|
| 2189 |
-
|
|
|
|
|
|
|
| 2190 |
implicit `this` parameter shall be treated as if it were a pointer to
|
| 2191 |
the derived class rather than to the base class. This has no effect on
|
| 2192 |
the type of the function, and in all other respects the function remains
|
| 2193 |
-
a member of the base class.
|
| 2194 |
-
|
| 2195 |
-
|
| 2196 |
-
[[class.
|
| 2197 |
-
|
| 2198 |
-
|
| 2199 |
-
|
| 2200 |
-
|
| 2201 |
-
class
|
| 2202 |
-
|
| 2203 |
-
*using-
|
| 2204 |
-
|
| 2205 |
-
|
| 2206 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2207 |
|
| 2208 |
``` cpp
|
| 2209 |
struct A { int x(); };
|
| 2210 |
struct B : A { };
|
| 2211 |
struct C : A {
|
|
@@ -2216,18 +2729,26 @@ struct C : A {
|
|
| 2216 |
struct D : B, C {
|
| 2217 |
using C::x;
|
| 2218 |
int x(double);
|
| 2219 |
};
|
| 2220 |
int f(D* d) {
|
| 2221 |
-
return d->x(); //
|
| 2222 |
}
|
| 2223 |
```
|
| 2224 |
|
| 2225 |
-
|
| 2226 |
-
|
| 2227 |
-
|
| 2228 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2229 |
|
| 2230 |
``` cpp
|
| 2231 |
class A {
|
| 2232 |
private:
|
| 2233 |
void f(char);
|
|
@@ -2242,11 +2763,13 @@ class B : public A {
|
|
| 2242 |
public:
|
| 2243 |
using A::g; // B::g is a public synonym for A::g
|
| 2244 |
};
|
| 2245 |
```
|
| 2246 |
|
| 2247 |
-
|
|
|
|
|
|
|
| 2248 |
dependent name ([[temp.dep]]), the name introduced by the
|
| 2249 |
*using-declaration* is treated as a *typedef-name* ([[dcl.typedef]]).
|
| 2250 |
|
| 2251 |
### Using directive <a id="namespace.udir">[[namespace.udir]]</a>
|
| 2252 |
|
|
@@ -2254,26 +2777,34 @@ dependent name ([[temp.dep]]), the name introduced by the
|
|
| 2254 |
using-directive:
|
| 2255 |
attribute-specifier-seqₒₚₜ 'using namespace' nested-name-specifierₒₚₜ namespace-name ';'
|
| 2256 |
```
|
| 2257 |
|
| 2258 |
A *using-directive* shall not appear in class scope, but may appear in
|
| 2259 |
-
namespace scope or in block scope.
|
| 2260 |
-
|
| 2261 |
-
[
|
| 2262 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2263 |
|
| 2264 |
A *using-directive* specifies that the names in the nominated namespace
|
| 2265 |
can be used in the scope in which the *using-directive* appears after
|
| 2266 |
the *using-directive*. During unqualified name lookup (
|
| 2267 |
[[basic.lookup.unqual]]), the names appear as if they were declared in
|
| 2268 |
the nearest enclosing namespace which contains both the
|
| 2269 |
-
*using-directive* and the nominated namespace.
|
| 2270 |
-
|
|
|
|
|
|
|
| 2271 |
|
| 2272 |
A *using-directive* does not add any members to the declarative region
|
| 2273 |
in which it appears.
|
| 2274 |
|
|
|
|
|
|
|
| 2275 |
``` cpp
|
| 2276 |
namespace A {
|
| 2277 |
int i;
|
| 2278 |
namespace B {
|
| 2279 |
namespace C {
|
|
@@ -2298,15 +2829,22 @@ namespace A {
|
|
| 2298 |
void f4() {
|
| 2299 |
i = 5; // ill-formed; neither i is visible
|
| 2300 |
}
|
| 2301 |
```
|
| 2302 |
|
|
|
|
|
|
|
| 2303 |
For unqualified lookup ([[basic.lookup.unqual]]), the *using-directive*
|
| 2304 |
is transitive: if a scope contains a *using-directive* that nominates a
|
| 2305 |
second namespace that itself contains *using-directive*s, the effect is
|
| 2306 |
as if the *using-directive*s from the second namespace also appeared in
|
| 2307 |
-
the first.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2308 |
|
| 2309 |
``` cpp
|
| 2310 |
namespace M {
|
| 2311 |
int i;
|
| 2312 |
}
|
|
@@ -2345,21 +2883,27 @@ namespace B {
|
|
| 2345 |
int n = j; // D::j hides B::j
|
| 2346 |
}
|
| 2347 |
}
|
| 2348 |
```
|
| 2349 |
|
| 2350 |
-
|
| 2351 |
-
|
| 2352 |
-
|
| 2353 |
-
*using-directive*
|
| 2354 |
-
|
|
|
|
|
|
|
| 2355 |
|
| 2356 |
If name lookup finds a declaration for a name in two different
|
| 2357 |
namespaces, and the declarations do not declare the same entity and do
|
| 2358 |
-
not declare functions, the use of the name is ill-formed.
|
| 2359 |
-
|
| 2360 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2361 |
|
| 2362 |
``` cpp
|
| 2363 |
namespace A {
|
| 2364 |
class X { };
|
| 2365 |
extern "C" int g();
|
|
@@ -2373,24 +2917,31 @@ namespace B {
|
|
| 2373 |
using namespace A;
|
| 2374 |
using namespace B;
|
| 2375 |
|
| 2376 |
void f() {
|
| 2377 |
X(1); // error: name X found in two namespaces
|
| 2378 |
-
g(); //
|
| 2379 |
-
h(); //
|
| 2380 |
}
|
| 2381 |
```
|
| 2382 |
|
|
|
|
|
|
|
| 2383 |
During overload resolution, all functions from the transitive search are
|
| 2384 |
considered for argument matching. The set of declarations found by the
|
| 2385 |
-
transitive search is unordered.
|
| 2386 |
-
|
| 2387 |
-
|
| 2388 |
-
|
| 2389 |
-
|
| 2390 |
-
|
| 2391 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2392 |
|
| 2393 |
``` cpp
|
| 2394 |
namespace D {
|
| 2395 |
int d1;
|
| 2396 |
void f(char);
|
|
@@ -2419,35 +2970,44 @@ void f() {
|
|
| 2419 |
f(1); // error: ambiguous: D::f(int) or E::f(int)?
|
| 2420 |
f('a'); // OK: D::f(char)
|
| 2421 |
}
|
| 2422 |
```
|
| 2423 |
|
|
|
|
|
|
|
| 2424 |
## The `asm` declaration <a id="dcl.asm">[[dcl.asm]]</a>
|
| 2425 |
|
| 2426 |
An `asm` declaration has the form
|
| 2427 |
|
| 2428 |
``` bnf
|
| 2429 |
asm-definition:
|
| 2430 |
-
'asm (' string-literal ') ;'
|
| 2431 |
```
|
| 2432 |
|
| 2433 |
The `asm` declaration is conditionally-supported; its meaning is
|
| 2434 |
-
*implementation-defined*.
|
| 2435 |
-
|
|
|
|
|
|
|
|
|
|
| 2436 |
|
| 2437 |
## Linkage specifications <a id="dcl.link">[[dcl.link]]</a>
|
| 2438 |
|
| 2439 |
All function types, function names with external linkage, and variable
|
| 2440 |
-
names with external linkage have a *language linkage*.
|
| 2441 |
-
|
| 2442 |
-
|
| 2443 |
-
|
| 2444 |
-
|
| 2445 |
-
with a particular
|
| 2446 |
-
|
| 2447 |
-
|
| 2448 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2449 |
|
| 2450 |
Linkage ([[basic.link]]) between C++and non-C++code fragments can be
|
| 2451 |
achieved using a *linkage-specification*:
|
| 2452 |
|
| 2453 |
``` bnf
|
|
@@ -2458,103 +3018,112 @@ linkage-specification:
|
|
| 2458 |
|
| 2459 |
The *string-literal* indicates the required language linkage. This
|
| 2460 |
International Standard specifies the semantics for the *string-literal*s
|
| 2461 |
`"C"` and `"C++"`. Use of a *string-literal* other than `"C"` or `"C++"`
|
| 2462 |
is conditionally-supported, with *implementation-defined* semantics.
|
| 2463 |
-
|
| 2464 |
-
|
| 2465 |
-
that
|
| 2466 |
-
|
| 2467 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2468 |
|
| 2469 |
Every implementation shall provide for linkage to functions written in
|
| 2470 |
the C programming language, `"C"`, and linkage to C++functions, `"C++"`.
|
| 2471 |
|
|
|
|
|
|
|
| 2472 |
``` cpp
|
| 2473 |
complex sqrt(complex); // C++linkage by default
|
| 2474 |
extern "C" {
|
| 2475 |
double sqrt(double); // C linkage
|
| 2476 |
}
|
| 2477 |
```
|
| 2478 |
|
|
|
|
|
|
|
| 2479 |
Linkage specifications nest. When linkage specifications nest, the
|
| 2480 |
innermost one determines the language linkage. A linkage specification
|
| 2481 |
does not establish a scope. A *linkage-specification* shall occur only
|
| 2482 |
in namespace scope ([[basic.scope]]). In a *linkage-specification*, the
|
| 2483 |
specified language linkage applies to the function types of all function
|
| 2484 |
declarators, function names with external linkage, and variable names
|
| 2485 |
with external linkage declared within the *linkage-specification*.
|
| 2486 |
|
|
|
|
|
|
|
| 2487 |
``` cpp
|
| 2488 |
-
extern "C"
|
| 2489 |
-
|
| 2490 |
-
|
| 2491 |
extern "C" typedef void FUNC();
|
| 2492 |
FUNC f2; // the name f2 has C++language linkage and the
|
| 2493 |
// function's type has C language linkage
|
| 2494 |
-
|
| 2495 |
-
|
| 2496 |
-
|
| 2497 |
-
|
| 2498 |
-
//
|
|
|
|
| 2499 |
extern "C" {
|
| 2500 |
-
static void f4(); // the name of the function f4 has
|
| 2501 |
-
//
|
| 2502 |
-
// linkage) and the function's type
|
| 2503 |
-
// has C language linkage.
|
| 2504 |
}
|
| 2505 |
|
| 2506 |
extern "C" void f5() {
|
| 2507 |
-
extern void f4(); // OK: Name linkage (internal)
|
| 2508 |
-
//
|
| 2509 |
-
// language linkage) obtained from
|
| 2510 |
-
// previous declaration.
|
| 2511 |
}
|
| 2512 |
|
| 2513 |
-
extern void f4(); // OK: Name linkage (internal)
|
| 2514 |
-
//
|
| 2515 |
-
// language linkage) obtained from
|
| 2516 |
-
// previous declaration.
|
| 2517 |
|
| 2518 |
void f6() {
|
| 2519 |
-
extern void f4(); // OK: Name linkage (internal)
|
| 2520 |
-
//
|
| 2521 |
-
// language linkage) obtained from
|
| 2522 |
-
// previous declaration.
|
| 2523 |
}
|
| 2524 |
```
|
| 2525 |
|
|
|
|
|
|
|
| 2526 |
A C language linkage is ignored in determining the language linkage of
|
| 2527 |
the names of class members and the function type of class member
|
| 2528 |
functions.
|
| 2529 |
|
|
|
|
|
|
|
| 2530 |
``` cpp
|
| 2531 |
extern "C" typedef void FUNC_c();
|
|
|
|
| 2532 |
class C {
|
| 2533 |
-
|
| 2534 |
-
//
|
| 2535 |
-
|
| 2536 |
-
|
| 2537 |
-
//
|
| 2538 |
-
|
| 2539 |
-
|
| 2540 |
-
// C function
|
| 2541 |
};
|
| 2542 |
|
| 2543 |
extern "C" {
|
| 2544 |
class X {
|
| 2545 |
-
|
| 2546 |
-
//
|
| 2547 |
-
|
| 2548 |
-
//
|
| 2549 |
-
// C function
|
| 2550 |
};
|
| 2551 |
}
|
| 2552 |
```
|
| 2553 |
|
|
|
|
|
|
|
| 2554 |
If two declarations declare functions with the same name and
|
| 2555 |
-
|
| 2556 |
or declare objects with the same name to be members of the same
|
| 2557 |
namespace and the declarations give the names different language
|
| 2558 |
linkages, the program is ill-formed; no diagnostic is required if the
|
| 2559 |
declarations appear in different translation units. Except for functions
|
| 2560 |
with C++linkage, a function declaration without a linkage specification
|
|
@@ -2569,20 +3138,24 @@ Two declarations for a function with C language linkage with the same
|
|
| 2569 |
function name (ignoring the namespace names that qualify it) that appear
|
| 2570 |
in different namespace scopes refer to the same function. Two
|
| 2571 |
declarations for a variable with C language linkage with the same name
|
| 2572 |
(ignoring the namespace names that qualify it) that appear in different
|
| 2573 |
namespace scopes refer to the same variable. An entity with C language
|
| 2574 |
-
linkage shall not be declared with the same name as
|
| 2575 |
scope, unless both declarations denote the same entity; no diagnostic is
|
| 2576 |
required if the declarations appear in different translation units. A
|
| 2577 |
variable with C language linkage shall not be declared with the same
|
| 2578 |
name as a function with C language linkage (ignoring the namespace names
|
| 2579 |
that qualify the respective names); no diagnostic is required if the
|
| 2580 |
-
declarations appear in different translation units.
|
| 2581 |
-
|
| 2582 |
-
|
| 2583 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2584 |
|
| 2585 |
``` cpp
|
| 2586 |
int x;
|
| 2587 |
namespace A {
|
| 2588 |
extern "C" int f();
|
|
@@ -2591,40 +3164,45 @@ namespace A {
|
|
| 2591 |
extern "C" int x(); // ill-formed: same name as global-space object x
|
| 2592 |
}
|
| 2593 |
|
| 2594 |
namespace B {
|
| 2595 |
extern "C" int f(); // A::f and B::f refer to the same function
|
| 2596 |
-
extern "C" int g() { return 1; } // ill-formed, the function g
|
| 2597 |
-
// with C language linkage has two definitions
|
| 2598 |
}
|
| 2599 |
|
| 2600 |
int A::f() { return 98; } // definition for the function f with C language linkage
|
| 2601 |
extern "C" int h() { return 97; } // definition for the function h with C language linkage
|
| 2602 |
// A::h and ::h refer to the same function
|
| 2603 |
```
|
| 2604 |
|
|
|
|
|
|
|
| 2605 |
A declaration directly contained in a *linkage-specification* is treated
|
| 2606 |
as if it contains the `extern` specifier ([[dcl.stc]]) for the purpose
|
| 2607 |
of determining the linkage of the declared name and whether it is a
|
| 2608 |
definition. Such a declaration shall not specify a storage class.
|
| 2609 |
|
|
|
|
|
|
|
| 2610 |
``` cpp
|
| 2611 |
extern "C" double f();
|
| 2612 |
static double f(); // error
|
| 2613 |
extern "C" int i; // declaration
|
| 2614 |
extern "C" {
|
| 2615 |
int i; // definition
|
| 2616 |
}
|
| 2617 |
extern "C" static void g(); // error
|
| 2618 |
```
|
| 2619 |
|
| 2620 |
-
|
| 2621 |
-
|
| 2622 |
-
|
|
|
|
|
|
|
| 2623 |
|
| 2624 |
Linkage from C++to objects defined in other languages and to objects
|
| 2625 |
-
defined in C++from other languages is implementation-defined and
|
| 2626 |
language-dependent. Only where the object layout strategies of two
|
| 2627 |
language implementations are similar enough can such linkage be
|
| 2628 |
achieved.
|
| 2629 |
|
| 2630 |
## Attributes <a id="dcl.attr">[[dcl.attr]]</a>
|
|
@@ -2639,20 +3217,25 @@ attribute-specifier-seq:
|
|
| 2639 |
attribute-specifier-seqₒₚₜ attribute-specifier
|
| 2640 |
```
|
| 2641 |
|
| 2642 |
``` bnf
|
| 2643 |
attribute-specifier:
|
| 2644 |
-
'[' '[' attribute-list ']' ']'
|
| 2645 |
alignment-specifier
|
| 2646 |
```
|
| 2647 |
|
| 2648 |
``` bnf
|
| 2649 |
alignment-specifier:
|
| 2650 |
'alignas (' type-id '...'ₒₚₜ ')'
|
| 2651 |
'alignas (' constant-expression '...'ₒₚₜ ')'
|
| 2652 |
```
|
| 2653 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2654 |
``` bnf
|
| 2655 |
attribute-list:
|
| 2656 |
attributeₒₚₜ
|
| 2657 |
attribute-list ',' attributeₒₚₜ
|
| 2658 |
attribute '...'
|
|
@@ -2680,84 +3263,114 @@ attribute-namespace:
|
|
| 2680 |
identifier
|
| 2681 |
```
|
| 2682 |
|
| 2683 |
``` bnf
|
| 2684 |
attribute-argument-clause:
|
| 2685 |
-
'(' balanced-token-seq ')'
|
| 2686 |
```
|
| 2687 |
|
| 2688 |
``` bnf
|
| 2689 |
balanced-token-seq:
|
| 2690 |
-
balanced-token
|
| 2691 |
balanced-token-seq balanced-token
|
| 2692 |
```
|
| 2693 |
|
| 2694 |
``` bnf
|
| 2695 |
balanced-token:
|
| 2696 |
-
'(' balanced-token-seq ')'
|
| 2697 |
-
'[' balanced-token-seq ']'
|
| 2698 |
-
'{' balanced-token-seq '}'
|
| 2699 |
any *token* other than a parenthesis, a bracket, or a brace
|
| 2700 |
```
|
| 2701 |
|
| 2702 |
-
|
| 2703 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2704 |
|
| 2705 |
In an *attribute-list*, an ellipsis may appear only if that
|
| 2706 |
*attribute*’s specification permits it. An *attribute* followed by an
|
| 2707 |
ellipsis is a pack expansion ([[temp.variadic]]). An
|
| 2708 |
*attribute-specifier* that contains no *attribute*s has no effect. The
|
| 2709 |
-
order in which the *attribute-
|
| 2710 |
not significant. If a keyword ([[lex.key]]) or an alternative token (
|
| 2711 |
[[lex.digraph]]) that satisfies the syntactic requirements of an
|
| 2712 |
*identifier* ([[lex.name]]) is contained in an *attribute-token*, it is
|
| 2713 |
considered an identifier. No name lookup ([[basic.lookup]]) is
|
| 2714 |
performed on any of the identifiers contained in an *attribute-token*.
|
| 2715 |
The *attribute-token* determines additional requirements on the
|
| 2716 |
-
*attribute-argument-clause* (if any).
|
| 2717 |
-
*attribute-scoped-token* is conditionally-supported, with
|
| 2718 |
-
*implementation-defined* behavior. Each implementation should choose a
|
| 2719 |
-
distinctive name for the *attribute-namespace* in an
|
| 2720 |
-
*attribute-scoped-token*.
|
| 2721 |
|
| 2722 |
Each *attribute-specifier-seq* is said to *appertain* to some entity or
|
| 2723 |
statement, identified by the syntactic context where it appears (Clause
|
| 2724 |
[[stmt.stmt]], Clause [[dcl.dcl]], Clause [[dcl.decl]]). If an
|
| 2725 |
*attribute-specifier-seq* that appertains to some entity or statement
|
| 2726 |
-
contains an *attribute* that is not allowed to
|
| 2727 |
-
statement, the program is ill-formed. If an
|
| 2728 |
-
appertains to a friend declaration (
|
| 2729 |
-
shall be a definition. No
|
| 2730 |
-
an explicit instantiation (
|
|
|
|
| 2731 |
|
| 2732 |
-
For an *attribute-token*
|
| 2733 |
-
the behavior is
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2734 |
|
| 2735 |
Two consecutive left square bracket tokens shall appear only when
|
| 2736 |
-
introducing an *attribute-specifier*
|
| 2737 |
-
|
| 2738 |
-
|
| 2739 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2740 |
|
| 2741 |
``` cpp
|
| 2742 |
int p[10];
|
| 2743 |
void f() {
|
| 2744 |
int x = 42, y[5];
|
| 2745 |
-
int(p[[x] { return x; }()]); // error: invalid attribute on a nested
|
| 2746 |
-
//
|
| 2747 |
-
|
| 2748 |
-
|
| 2749 |
-
// in this context.
|
| 2750 |
}
|
| 2751 |
```
|
| 2752 |
|
|
|
|
|
|
|
| 2753 |
### Alignment specifier <a id="dcl.align">[[dcl.align]]</a>
|
| 2754 |
|
| 2755 |
An *alignment-specifier* may be applied to a variable or to a class data
|
| 2756 |
member, but it shall not be applied to a bit-field, a function
|
| 2757 |
-
parameter, an *exception-declaration* ([[except.handle]])
|
| 2758 |
-
variable declared with the `register` storage class specifier. An
|
| 2759 |
*alignment-specifier* may also be applied to the declaration or
|
| 2760 |
definition of a class (in an *elaborated-type-specifier* (
|
| 2761 |
[[dcl.type.elab]]) or *class-head* (Clause [[class]]), respectively)
|
| 2762 |
and to the declaration or definition of an enumeration (in an
|
| 2763 |
*opaque-enum-declaration* or *enum-head*, respectively ([[dcl.enum]])).
|
|
@@ -2766,103 +3379,85 @@ An *alignment-specifier* with an ellipsis is a pack expansion (
|
|
| 2766 |
|
| 2767 |
When the *alignment-specifier* is of the form `alignas(`
|
| 2768 |
*constant-expression* `)`:
|
| 2769 |
|
| 2770 |
- the *constant-expression* shall be an integral constant expression
|
| 2771 |
-
- if the constant expression
|
| 2772 |
-
|
| 2773 |
-
fundamental alignment
|
| 2774 |
-
- if the constant expression evaluates to an extended alignment and the
|
| 2775 |
-
implementation supports that alignment in the context of the
|
| 2776 |
-
declaration, the alignment of the declared entity shall be that
|
| 2777 |
-
alignment
|
| 2778 |
-
- if the constant expression evaluates to an extended alignment and the
|
| 2779 |
implementation does not support that alignment in the context of the
|
| 2780 |
-
declaration, the program is ill-formed
|
| 2781 |
-
- if the constant expression evaluates to zero, the alignment specifier
|
| 2782 |
-
shall have no effect
|
| 2783 |
-
- otherwise, the program is ill-formed.
|
| 2784 |
|
| 2785 |
-
|
| 2786 |
-
|
| 2787 |
-
[[expr.alignof]]).
|
| 2788 |
|
| 2789 |
-
|
| 2790 |
-
alignment
|
|
|
|
| 2791 |
|
| 2792 |
The combined effect of all *alignment-specifier*s in a declaration shall
|
| 2793 |
not specify an alignment that is less strict than the alignment that
|
| 2794 |
would be required for the entity being declared if all
|
| 2795 |
-
*alignment-specifier*s
|
| 2796 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2797 |
|
| 2798 |
If the defining declaration of an entity has an *alignment-specifier*,
|
| 2799 |
any non-defining declaration of that entity shall either specify
|
| 2800 |
equivalent alignment or have no *alignment-specifier*. Conversely, if
|
| 2801 |
any declaration of an entity has an *alignment-specifier*, every
|
| 2802 |
defining declaration of that entity shall specify an equivalent
|
| 2803 |
alignment. No diagnostic is required if declarations of an entity have
|
| 2804 |
different *alignment-specifier*s in different translation units.
|
| 2805 |
|
|
|
|
|
|
|
| 2806 |
``` cpp
|
| 2807 |
// Translation unit #1:
|
| 2808 |
-
struct S { int x; } s, p = &s;
|
| 2809 |
|
| 2810 |
// Translation unit #2:
|
| 2811 |
-
struct alignas(16) S; // error: definition of S lacks alignment
|
| 2812 |
-
extern S* p;
|
| 2813 |
```
|
| 2814 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2815 |
An aligned buffer with an alignment requirement of `A` and holding `N`
|
| 2816 |
-
elements of type `T`
|
| 2817 |
-
`unsigned char` can be declared as:
|
| 2818 |
|
| 2819 |
``` cpp
|
| 2820 |
alignas(T) alignas(A) T buffer[N];
|
| 2821 |
```
|
| 2822 |
|
| 2823 |
Specifying `alignas(T)` ensures that the final requested alignment will
|
| 2824 |
not be weaker than `alignof(T)`, and therefore the program will not be
|
| 2825 |
ill-formed.
|
| 2826 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2827 |
``` cpp
|
| 2828 |
alignas(double) void f(); // error: alignment applied to function
|
| 2829 |
alignas(double) unsigned char c[sizeof(double)]; // array of characters, suitably aligned for a double
|
| 2830 |
extern unsigned char c[sizeof(double)]; // no alignas necessary
|
| 2831 |
alignas(float)
|
| 2832 |
extern unsigned char c[sizeof(double)]; // error: different alignment in declaration
|
| 2833 |
```
|
| 2834 |
|
| 2835 |
-
|
| 2836 |
-
|
| 2837 |
-
The *attribute-token* `noreturn` specifies that a function does not
|
| 2838 |
-
return. It shall appear at most once in each *attribute-list* and no
|
| 2839 |
-
*attribute-argument-clause* shall be present. The attribute may be
|
| 2840 |
-
applied to the *declarator-id* in a function declaration. The first
|
| 2841 |
-
declaration of a function shall specify the `noreturn` attribute if any
|
| 2842 |
-
declaration of that function specifies the `noreturn` attribute. If a
|
| 2843 |
-
function is declared with the `noreturn` attribute in one translation
|
| 2844 |
-
unit and the same function is declared without the `noreturn` attribute
|
| 2845 |
-
in another translation unit, the program is ill-formed; no diagnostic
|
| 2846 |
-
required.
|
| 2847 |
-
|
| 2848 |
-
If a function `f` is called where `f` was previously declared with the
|
| 2849 |
-
`noreturn` attribute and `f` eventually returns, the behavior is
|
| 2850 |
-
undefined. The function may terminate by throwing an exception.
|
| 2851 |
-
Implementations are encouraged to issue a warning if a function marked
|
| 2852 |
-
`[[noreturn]]` might return.
|
| 2853 |
-
|
| 2854 |
-
``` cpp
|
| 2855 |
-
[[ noreturn ]] void f() {
|
| 2856 |
-
throw "error"; // OK
|
| 2857 |
-
}
|
| 2858 |
-
|
| 2859 |
-
[[ noreturn ]] void q(int i) { // behavior is undefined if called with an argument <= 0
|
| 2860 |
-
if (i > 0)
|
| 2861 |
-
throw "positive";
|
| 2862 |
-
}
|
| 2863 |
-
```
|
| 2864 |
|
| 2865 |
### Carries dependency attribute <a id="dcl.attr.depend">[[dcl.attr.depend]]</a>
|
| 2866 |
|
| 2867 |
The *attribute-token* `carries_dependency` specifies dependency
|
| 2868 |
propagation into and out of functions. It shall appear at most once in
|
|
@@ -2885,14 +3480,17 @@ declaration of that function specifies the `carries_dependency`
|
|
| 2885 |
attribute for that parameter. If a function or one of its parameters is
|
| 2886 |
declared with the `carries_dependency` attribute in its first
|
| 2887 |
declaration in one translation unit and the same function or one of its
|
| 2888 |
parameters is declared without the `carries_dependency` attribute in its
|
| 2889 |
first declaration in another translation unit, the program is
|
| 2890 |
-
ill-formed
|
| 2891 |
|
| 2892 |
-
The `carries_dependency` attribute does not change the
|
| 2893 |
-
program, but may result in generation of more efficient
|
|
|
|
|
|
|
|
|
|
| 2894 |
|
| 2895 |
``` cpp
|
| 2896 |
/* Translation unit A. */
|
| 2897 |
|
| 2898 |
struct foo { int* a; int* b; };
|
|
@@ -2933,42 +3531,194 @@ Function `g`’s second parameter has a `carries_dependency` attribute,
|
|
| 2933 |
but its first parameter does not. Therefore, function `h`’s first call
|
| 2934 |
to `g` carries a dependency into `g`, but its second call does not. The
|
| 2935 |
implementation might need to insert a fence prior to the second call to
|
| 2936 |
`g`.
|
| 2937 |
|
|
|
|
|
|
|
| 2938 |
### Deprecated attribute <a id="dcl.attr.deprecated">[[dcl.attr.deprecated]]</a>
|
| 2939 |
|
| 2940 |
The *attribute-token* `deprecated` can be used to mark names and
|
| 2941 |
entities whose use is still allowed, but is discouraged for some reason.
|
| 2942 |
-
|
| 2943 |
-
|
| 2944 |
-
|
| 2945 |
-
|
|
|
|
|
|
|
|
|
|
| 2946 |
|
| 2947 |
``` cpp
|
| 2948 |
( string-literal )
|
| 2949 |
```
|
| 2950 |
|
| 2951 |
-
|
| 2952 |
-
explain the rationale for deprecation and/or to suggest
|
| 2953 |
-
entity.
|
| 2954 |
|
| 2955 |
The attribute may be applied to the declaration of a class, a
|
| 2956 |
-
*typedef-name*, a variable, a non-static data member, a function,
|
| 2957 |
-
enumeration, or a template specialization.
|
| 2958 |
|
| 2959 |
A name or entity declared without the `deprecated` attribute can later
|
| 2960 |
-
be
|
| 2961 |
-
|
| 2962 |
-
|
| 2963 |
-
|
|
|
|
|
|
|
|
|
|
| 2964 |
Redeclarations using different forms of the attribute (with or without
|
| 2965 |
the *attribute-argument-clause* or with different
|
| 2966 |
*attribute-argument-clause*s) are allowed.
|
| 2967 |
|
| 2968 |
-
Implementations may use the `deprecated
|
| 2969 |
-
diagnostic message in case the program refers to a name or
|
| 2970 |
-
than to declare it, after a declaration that specifies the
|
| 2971 |
-
The diagnostic message may include the text provided within
|
| 2972 |
-
*attribute-argument-clause* of any `deprecated` attribute applied to
|
| 2973 |
-
name or entity.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2974 |
|
|
|
|
| 10 |
```
|
| 11 |
|
| 12 |
``` bnf
|
| 13 |
declaration:
|
| 14 |
block-declaration
|
| 15 |
+
nodeclspec-function-declaration
|
| 16 |
function-definition
|
| 17 |
template-declaration
|
| 18 |
+
deduction-guide
|
| 19 |
explicit-instantiation
|
| 20 |
explicit-specialization
|
| 21 |
linkage-specification
|
| 22 |
namespace-definition
|
| 23 |
empty-declaration
|
|
|
|
| 34 |
static_assert-declaration
|
| 35 |
alias-declaration
|
| 36 |
opaque-enum-declaration
|
| 37 |
```
|
| 38 |
|
| 39 |
+
``` bnf
|
| 40 |
+
nodeclspec-function-declaration:
|
| 41 |
+
attribute-specifier-seqₒₚₜ declarator ';'
|
| 42 |
+
```
|
| 43 |
+
|
| 44 |
``` bnf
|
| 45 |
alias-declaration:
|
| 46 |
+
'using' identifier attribute-specifier-seqₒₚₜ '=' defining-type-id ';'
|
| 47 |
```
|
| 48 |
|
| 49 |
``` bnf
|
| 50 |
simple-declaration:
|
| 51 |
+
decl-specifier-seq init-declarator-listₒₚₜ ';'
|
| 52 |
+
attribute-specifier-seq decl-specifier-seq init-declarator-list ';'
|
| 53 |
+
attribute-specifier-seqₒₚₜ decl-specifier-seq ref-qualifierₒₚₜ '[' identifier-list ']' initializer ';'
|
| 54 |
```
|
| 55 |
|
| 56 |
``` bnf
|
| 57 |
static_assert-declaration:
|
| 58 |
+
'static_assert' '(' constant-expression ')' ';'
|
| 59 |
'static_assert' '(' constant-expression ',' string-literal ')' ';'
|
| 60 |
```
|
| 61 |
|
| 62 |
``` bnf
|
| 63 |
empty-declaration:
|
|
|
|
| 67 |
``` bnf
|
| 68 |
attribute-declaration:
|
| 69 |
attribute-specifier-seq ';'
|
| 70 |
```
|
| 71 |
|
| 72 |
+
[*Note 1*: *asm-definition*s are described in [[dcl.asm]], and
|
| 73 |
*linkage-specification*s are described in [[dcl.link]].
|
| 74 |
*Function-definition*s are described in [[dcl.fct.def]] and
|
| 75 |
+
*template-declaration*s and *deduction-guide*s are described in Clause
|
| 76 |
+
[[temp]]. *Namespace-definition*s are described in [[namespace.def]],
|
| 77 |
*using-declaration*s are described in [[namespace.udecl]] and
|
| 78 |
+
*using-directive*s are described in [[namespace.udir]]. — *end note*]
|
| 79 |
|
| 80 |
+
A *simple-declaration* or *nodeclspec-function-declaration* of the form
|
| 81 |
|
| 82 |
``` bnf
|
| 83 |
attribute-specifier-seqₒₚₜ decl-specifier-seqₒₚₜ init-declarator-listₒₚₜ ';'
|
| 84 |
```
|
| 85 |
|
| 86 |
is divided into three parts. Attributes are described in [[dcl.attr]].
|
| 87 |
*decl-specifier*s, the principal components of a *decl-specifier-seq*,
|
| 88 |
are described in [[dcl.spec]]. *declarator*s, the components of an
|
| 89 |
*init-declarator-list*, are described in Clause [[dcl.decl]]. The
|
| 90 |
+
*attribute-specifier-seq* appertains to each of the entities declared by
|
| 91 |
+
the *declarator*s of the *init-declarator-list*.
|
| 92 |
+
|
| 93 |
+
[*Note 2*: In the declaration for an entity, attributes appertaining to
|
| 94 |
+
that entity may appear at the start of the declaration and after the
|
| 95 |
+
*declarator-id* for that declaration. — *end note*]
|
| 96 |
+
|
| 97 |
+
[*Example 1*:
|
| 98 |
|
| 99 |
``` cpp
|
| 100 |
[[noreturn]] void f [[noreturn]] (); // OK
|
| 101 |
```
|
| 102 |
|
| 103 |
+
— *end example*]
|
| 104 |
+
|
| 105 |
Except where otherwise specified, the meaning of an
|
| 106 |
*attribute-declaration* is *implementation-defined*.
|
| 107 |
|
| 108 |
A declaration occurs in a scope ([[basic.scope]]); the scope rules are
|
| 109 |
summarized in [[basic.lookup]]. A declaration that declares a function
|
|
|
|
| 125 |
names being declared by the declaration (as *class-name*s, *enum-name*s,
|
| 126 |
or *enumerator*s, depending on the syntax). In such cases, the
|
| 127 |
*decl-specifier-seq* shall introduce one or more names into the program,
|
| 128 |
or shall redeclare a name introduced by a previous declaration.
|
| 129 |
|
| 130 |
+
[*Example 2*:
|
| 131 |
+
|
| 132 |
``` cpp
|
| 133 |
enum { }; // ill-formed
|
| 134 |
typedef class { }; // ill-formed
|
| 135 |
```
|
| 136 |
|
| 137 |
+
— *end example*]
|
| 138 |
+
|
| 139 |
+
In a *static_assert-declaration*, the *constant-expression* shall be a
|
| 140 |
+
contextually converted constant expression of type `bool` (
|
| 141 |
+
[[expr.const]]). If the value of the expression when so converted is
|
| 142 |
+
`true`, the declaration has no effect. Otherwise, the program is
|
| 143 |
+
ill-formed, and the resulting diagnostic message ([[intro.compliance]])
|
| 144 |
+
shall include the text of the *string-literal*, if one is supplied,
|
| 145 |
except that characters not in the basic source character set (
|
| 146 |
[[lex.charset]]) are not required to appear in the diagnostic message.
|
| 147 |
|
| 148 |
+
[*Example 3*:
|
| 149 |
+
|
| 150 |
``` cpp
|
| 151 |
+
static_assert(char(-1) < 0, "this library requires plain 'char' to be signed");
|
| 152 |
```
|
| 153 |
|
| 154 |
+
— *end example*]
|
| 155 |
+
|
| 156 |
An *empty-declaration* has no effect.
|
| 157 |
|
| 158 |
+
A *simple-declaration* with an *identifier-list* is called a *structured
|
| 159 |
+
binding declaration* ([[dcl.struct.bind]]). The *decl-specifier-seq*
|
| 160 |
+
shall contain only the *type-specifier* `auto` ([[dcl.spec.auto]]) and
|
| 161 |
+
*cv-qualifier*s. The *initializer* shall be of the form “`=`
|
| 162 |
+
*assignment-expression*”, of the form “`{` *assignment-expression* `}`”,
|
| 163 |
+
or of the form “`(` *assignment-expression* `)`”, where the
|
| 164 |
+
*assignment-expression* is of array or non-union class type.
|
| 165 |
+
|
| 166 |
Each *init-declarator* in the *init-declarator-list* contains exactly
|
| 167 |
one *declarator-id*, which is the name declared by that
|
| 168 |
*init-declarator* and hence one of the names declared by the
|
| 169 |
+
declaration. The *defining-type-specifier*s ([[dcl.type]]) in the
|
| 170 |
*decl-specifier-seq* and the recursive *declarator* structure of the
|
| 171 |
*init-declarator* describe a type ([[dcl.meaning]]), which is then
|
| 172 |
associated with the name being declared by the *init-declarator*.
|
| 173 |
|
| 174 |
If the *decl-specifier-seq* contains the `typedef` specifier, the
|
|
|
|
| 185 |
definition unless it contains the `extern` specifier and has no
|
| 186 |
initializer ([[basic.def]]). A definition causes the appropriate amount
|
| 187 |
of storage to be reserved and any appropriate initialization (
|
| 188 |
[[dcl.init]]) to be done.
|
| 189 |
|
| 190 |
+
A *nodeclspec-function-declaration* shall declare a constructor,
|
| 191 |
+
destructor, or conversion function.[^1]
|
| 192 |
+
|
| 193 |
+
[*Note 3*: A *nodeclspec-function-declaration* can only be used in a
|
| 194 |
+
*template-declaration* (Clause [[temp]]), *explicit-instantiation* (
|
| 195 |
+
[[temp.explicit]]), or *explicit-specialization* (
|
| 196 |
+
[[temp.expl.spec]]). — *end note*]
|
| 197 |
|
| 198 |
## Specifiers <a id="dcl.spec">[[dcl.spec]]</a>
|
| 199 |
|
| 200 |
The specifiers that can be used in a declaration are
|
| 201 |
|
| 202 |
``` bnf
|
| 203 |
decl-specifier:
|
| 204 |
storage-class-specifier
|
| 205 |
+
defining-type-specifier
|
| 206 |
function-specifier
|
| 207 |
'friend'
|
| 208 |
'typedef'
|
| 209 |
'constexpr'
|
| 210 |
+
'inline'
|
| 211 |
```
|
| 212 |
|
| 213 |
``` bnf
|
| 214 |
decl-specifier-seq:
|
| 215 |
decl-specifier attribute-specifier-seqₒₚₜ
|
|
|
|
| 220 |
appertains to the type determined by the preceding *decl-specifier*s (
|
| 221 |
[[dcl.meaning]]). The *attribute-specifier-seq* affects the type only
|
| 222 |
for the declaration it appears in, not other declarations involving the
|
| 223 |
same type.
|
| 224 |
|
| 225 |
+
Each *decl-specifier* shall appear at most once in a complete
|
| 226 |
+
*decl-specifier-seq*, except that `long` may appear twice.
|
| 227 |
+
|
| 228 |
If a *type-name* is encountered while parsing a *decl-specifier-seq*, it
|
| 229 |
is interpreted as part of the *decl-specifier-seq* if and only if there
|
| 230 |
+
is no previous *defining-type-specifier* other than a *cv-qualifier* in
|
| 231 |
+
the *decl-specifier-seq*. The sequence shall be self-consistent as
|
| 232 |
+
described below.
|
| 233 |
+
|
| 234 |
+
[*Example 1*:
|
| 235 |
|
| 236 |
``` cpp
|
| 237 |
typedef char* Pc;
|
| 238 |
static Pc; // error: name missing
|
| 239 |
```
|
|
|
|
| 248 |
``` cpp
|
| 249 |
void f(const Pc); // void f(char* const) (not const char*)
|
| 250 |
void g(const int Pc); // void g(const int)
|
| 251 |
```
|
| 252 |
|
| 253 |
+
— *end example*]
|
| 254 |
+
|
| 255 |
+
[*Note 1*:
|
| 256 |
+
|
| 257 |
Since `signed`, `unsigned`, `long`, and `short` by default imply `int`,
|
| 258 |
a *type-name* appearing after one of those specifiers is treated as the
|
| 259 |
name being (re)declared.
|
| 260 |
|
| 261 |
+
[*Example 2*:
|
| 262 |
+
|
| 263 |
``` cpp
|
| 264 |
void h(unsigned Pc); // void h(unsigned int)
|
| 265 |
void k(unsigned int Pc); // void k(unsigned int)
|
| 266 |
```
|
| 267 |
|
| 268 |
+
— *end example*]
|
| 269 |
+
|
| 270 |
+
— *end note*]
|
| 271 |
+
|
| 272 |
### Storage class specifiers <a id="dcl.stc">[[dcl.stc]]</a>
|
| 273 |
|
| 274 |
The storage class specifiers are
|
| 275 |
|
| 276 |
``` bnf
|
| 277 |
storage-class-specifier:
|
|
|
|
| 278 |
'static'
|
| 279 |
'thread_local'
|
| 280 |
'extern'
|
| 281 |
'mutable'
|
| 282 |
```
|
|
|
|
| 285 |
*decl-specifier-seq*, except that `thread_local` may appear with
|
| 286 |
`static` or `extern`. If `thread_local` appears in any declaration of a
|
| 287 |
variable it shall be present in all declarations of that entity. If a
|
| 288 |
*storage-class-specifier* appears in a *decl-specifier-seq*, there can
|
| 289 |
be no `typedef` specifier in the same *decl-specifier-seq* and the
|
| 290 |
+
*init-declarator-list* or *member-declarator-list* of the declaration
|
| 291 |
+
shall not be empty (except for an anonymous union declared in a named
|
| 292 |
+
namespace or in the global namespace, which shall be declared `static` (
|
| 293 |
+
[[class.union.anon]])). The *storage-class-specifier* applies to the
|
| 294 |
+
name declared by each *init-declarator* in the list and not to any names
|
| 295 |
+
declared by other specifiers. A *storage-class-specifier* other than
|
| 296 |
+
`thread_local` shall not be specified in an explicit specialization (
|
| 297 |
+
[[temp.expl.spec]]) or an explicit instantiation ([[temp.explicit]])
|
| 298 |
+
directive.
|
| 299 |
|
| 300 |
+
[*Note 1*: A variable declared without a *storage-class-specifier* at
|
| 301 |
+
block scope or declared as a function parameter has automatic storage
|
| 302 |
+
duration by default ([[basic.stc.auto]]). — *end note*]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 303 |
|
| 304 |
The `thread_local` specifier indicates that the named entity has thread
|
| 305 |
storage duration ([[basic.stc.thread]]). It shall be applied only to
|
| 306 |
the names of variables of namespace or block scope and to the names of
|
| 307 |
static data members. When `thread_local` is applied to a variable of
|
| 308 |
block scope the *storage-class-specifier* `static` is implied if no
|
| 309 |
other *storage-class-specifier* appears in the *decl-specifier-seq*.
|
| 310 |
|
| 311 |
The `static` specifier can be applied only to names of variables and
|
| 312 |
+
functions and to anonymous unions ([[class.union.anon]]). There can be
|
| 313 |
+
no `static` function declarations within a block, nor any `static`
|
| 314 |
+
function parameters. A `static` specifier used in the declaration of a
|
| 315 |
+
variable declares the variable to have static storage duration (
|
| 316 |
[[basic.stc.static]]), unless accompanied by the `thread_local`
|
| 317 |
specifier, which declares the variable to have thread storage duration (
|
| 318 |
[[basic.stc.thread]]). A `static` specifier can be used in declarations
|
| 319 |
of class members; [[class.static]] describes its effect. For the
|
| 320 |
linkage of a name declared with a `static` specifier, see
|
| 321 |
[[basic.link]].
|
| 322 |
|
| 323 |
The `extern` specifier can be applied only to the names of variables and
|
| 324 |
functions. The `extern` specifier cannot be used in the declaration of
|
| 325 |
class members or function parameters. For the linkage of a name declared
|
| 326 |
+
with an `extern` specifier, see [[basic.link]].
|
| 327 |
+
|
| 328 |
+
[*Note 2*: The `extern` keyword can also be used in
|
| 329 |
+
*explicit-instantiation*s and *linkage-specification*s, but it is not a
|
| 330 |
+
*storage-class-specifier* in such contexts. — *end note*]
|
| 331 |
|
| 332 |
The linkages implied by successive declarations for a given entity shall
|
| 333 |
agree. That is, within a given scope, each declaration declaring the
|
| 334 |
same variable name or the same overloading of a function name shall
|
| 335 |
imply the same linkage. Each function in a given set of overloaded
|
| 336 |
functions can have a different linkage, however.
|
| 337 |
|
| 338 |
+
[*Example 1*:
|
| 339 |
+
|
| 340 |
``` cpp
|
| 341 |
static char* f(); // f() has internal linkage
|
| 342 |
char* f() // f() still has internal linkage
|
| 343 |
+
{ ... }
|
| 344 |
|
| 345 |
char* g(); // g() has external linkage
|
| 346 |
static char* g() // error: inconsistent linkage
|
| 347 |
+
{ ... }
|
| 348 |
|
| 349 |
void h();
|
| 350 |
inline void h(); // external linkage
|
| 351 |
|
| 352 |
inline void l();
|
|
|
|
| 369 |
|
| 370 |
extern int d; // d has external linkage
|
| 371 |
static int d; // error: inconsistent linkage
|
| 372 |
```
|
| 373 |
|
| 374 |
+
— *end example*]
|
| 375 |
+
|
| 376 |
The name of a declared but undefined class can be used in an `extern`
|
| 377 |
declaration. Such a declaration can only be used in ways that do not
|
| 378 |
require a complete class type.
|
| 379 |
|
| 380 |
+
[*Example 2*:
|
| 381 |
+
|
| 382 |
``` cpp
|
| 383 |
struct S;
|
| 384 |
extern S a;
|
| 385 |
extern S f();
|
| 386 |
extern void g(S);
|
|
|
|
| 389 |
g(a); // error: S is incomplete
|
| 390 |
f(); // error: S is incomplete
|
| 391 |
}
|
| 392 |
```
|
| 393 |
|
| 394 |
+
— *end example*]
|
| 395 |
+
|
| 396 |
+
The `mutable` specifier shall appear only in the declaration of a
|
| 397 |
+
non-static data member ([[class.mem]]) whose type is neither
|
| 398 |
+
const-qualified nor a reference type.
|
| 399 |
+
|
| 400 |
+
[*Example 3*:
|
| 401 |
|
| 402 |
``` cpp
|
| 403 |
class X {
|
| 404 |
mutable const int* p; // OK
|
| 405 |
mutable int* const q; // ill-formed
|
| 406 |
};
|
| 407 |
```
|
| 408 |
|
| 409 |
+
— *end example*]
|
| 410 |
+
|
| 411 |
The `mutable` specifier on a class data member nullifies a `const`
|
| 412 |
specifier applied to the containing class object and permits
|
| 413 |
modification of the mutable class member even though the rest of the
|
| 414 |
object is `const` ([[dcl.type.cv]]).
|
| 415 |
|
|
|
|
| 417 |
|
| 418 |
can be used only in function declarations.
|
| 419 |
|
| 420 |
``` bnf
|
| 421 |
function-specifier:
|
|
|
|
| 422 |
'virtual'
|
| 423 |
'explicit'
|
| 424 |
```
|
| 425 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 426 |
The `virtual` specifier shall be used only in the initial declaration of
|
| 427 |
a non-static class member function; see [[class.virtual]].
|
| 428 |
|
| 429 |
The `explicit` specifier shall be used only in the declaration of a
|
| 430 |
constructor or conversion function within its class definition; see
|
|
|
|
| 434 |
|
| 435 |
Declarations containing the *decl-specifier* `typedef` declare
|
| 436 |
identifiers that can be used later for naming fundamental (
|
| 437 |
[[basic.fundamental]]) or compound ([[basic.compound]]) types. The
|
| 438 |
`typedef` specifier shall not be combined in a *decl-specifier-seq* with
|
| 439 |
+
any other kind of specifier except a *defining-type-specifier*, and it
|
| 440 |
+
shall not be used in the *decl-specifier-seq* of a
|
| 441 |
+
*parameter-declaration* ([[dcl.fct]]) nor in the *decl-specifier-seq*
|
| 442 |
+
of a *function-definition* ([[dcl.fct.def]]). If a `typedef` specifier
|
| 443 |
+
appears in a declaration without a *declarator*, the program is
|
| 444 |
+
ill-formed.
|
| 445 |
|
| 446 |
``` bnf
|
| 447 |
typedef-name:
|
| 448 |
identifier
|
| 449 |
```
|
|
|
|
| 452 |
Within the scope of its declaration, a *typedef-name* is syntactically
|
| 453 |
equivalent to a keyword and names the type associated with the
|
| 454 |
identifier in the way described in Clause [[dcl.decl]]. A
|
| 455 |
*typedef-name* is thus a synonym for another type. A *typedef-name* does
|
| 456 |
not introduce a new type the way a class declaration ([[class.name]])
|
| 457 |
+
or enum declaration does.
|
| 458 |
+
|
| 459 |
+
[*Example 1*:
|
| 460 |
+
|
| 461 |
+
After
|
| 462 |
|
| 463 |
``` cpp
|
| 464 |
typedef int MILES, *KLICKSP;
|
| 465 |
```
|
| 466 |
|
|
|
|
| 470 |
MILES distance;
|
| 471 |
extern KLICKSP metricp;
|
| 472 |
```
|
| 473 |
|
| 474 |
are all correct declarations; the type of `distance` is `int` and that
|
| 475 |
+
of `metricp` is “pointer to `int`”.
|
| 476 |
+
|
| 477 |
+
— *end example*]
|
| 478 |
|
| 479 |
A *typedef-name* can also be introduced by an *alias-declaration*. The
|
| 480 |
*identifier* following the `using` keyword becomes a *typedef-name* and
|
| 481 |
the optional *attribute-specifier-seq* following the *identifier*
|
| 482 |
+
appertains to that *typedef-name*. Such a *typedef-name* has the same
|
| 483 |
+
semantics as if it were introduced by the `typedef` specifier. In
|
| 484 |
+
particular, it does not define a new type.
|
| 485 |
+
|
| 486 |
+
[*Example 2*:
|
| 487 |
|
| 488 |
``` cpp
|
| 489 |
using handler_t = void (*)(int);
|
| 490 |
extern handler_t ignore;
|
| 491 |
extern void (*ignore)(int); // redeclare ignore
|
| 492 |
using cell = pair<void*, cell*>; // ill-formed
|
| 493 |
```
|
| 494 |
|
| 495 |
+
— *end example*]
|
| 496 |
+
|
| 497 |
+
The *defining-type-specifier-seq* of the *defining-type-id* shall not
|
| 498 |
+
define a class or enumeration if the *alias-declaration* is the
|
| 499 |
+
*declaration* of a *template-declaration*.
|
| 500 |
+
|
| 501 |
In a given non-class scope, a `typedef` specifier can be used to
|
| 502 |
redefine the name of any type declared in that scope to refer to the
|
| 503 |
type to which it already refers.
|
| 504 |
|
| 505 |
+
[*Example 3*:
|
| 506 |
+
|
| 507 |
``` cpp
|
| 508 |
+
typedef struct s { ... } s;
|
| 509 |
typedef int I;
|
| 510 |
typedef int I;
|
| 511 |
typedef I I;
|
| 512 |
```
|
| 513 |
|
| 514 |
+
— *end example*]
|
| 515 |
+
|
| 516 |
In a given class scope, a `typedef` specifier can be used to redefine
|
| 517 |
any *class-name* declared in that scope that is not also a
|
| 518 |
*typedef-name* to refer to the type to which it already refers.
|
| 519 |
|
| 520 |
+
[*Example 4*:
|
| 521 |
+
|
| 522 |
``` cpp
|
| 523 |
struct S {
|
| 524 |
typedef struct A { } A; // OK
|
| 525 |
typedef struct B B; // OK
|
| 526 |
typedef A A; // error
|
| 527 |
};
|
| 528 |
```
|
| 529 |
|
| 530 |
+
— *end example*]
|
| 531 |
+
|
| 532 |
If a `typedef` specifier is used to redefine in a given scope an entity
|
| 533 |
that can be referenced using an *elaborated-type-specifier*, the entity
|
| 534 |
can continue to be referenced by an *elaborated-type-specifier* or as an
|
| 535 |
enumeration or class name in an enumeration or class definition
|
| 536 |
respectively.
|
| 537 |
|
| 538 |
+
[*Example 5*:
|
| 539 |
+
|
| 540 |
``` cpp
|
| 541 |
struct S;
|
| 542 |
typedef struct S S;
|
| 543 |
int main() {
|
| 544 |
struct S* p; // OK
|
| 545 |
}
|
| 546 |
struct S { }; // OK
|
| 547 |
```
|
| 548 |
|
| 549 |
+
— *end example*]
|
| 550 |
+
|
| 551 |
In a given scope, a `typedef` specifier shall not be used to redefine
|
| 552 |
the name of any type declared in that scope to refer to a different
|
| 553 |
type.
|
| 554 |
|
| 555 |
+
[*Example 6*:
|
| 556 |
+
|
| 557 |
``` cpp
|
| 558 |
+
class complex { ... };
|
| 559 |
typedef int complex; // error: redefinition
|
| 560 |
```
|
| 561 |
|
| 562 |
+
— *end example*]
|
| 563 |
+
|
| 564 |
Similarly, in a given scope, a class or enumeration shall not be
|
| 565 |
declared with the same name as a *typedef-name* that is declared in that
|
| 566 |
scope and refers to a type other than the class or enumeration itself.
|
| 567 |
|
| 568 |
+
[*Example 7*:
|
| 569 |
+
|
| 570 |
``` cpp
|
| 571 |
typedef int complex;
|
| 572 |
+
class complex { ... }; // error: redefinition
|
| 573 |
```
|
| 574 |
|
| 575 |
+
— *end example*]
|
| 576 |
+
|
| 577 |
+
[*Note 1*: A *typedef-name* that names a class type, or a cv-qualified
|
| 578 |
+
version thereof, is also a *class-name* ([[class.name]]). If a
|
| 579 |
+
*typedef-name* is used to identify the subject of an
|
| 580 |
+
*elaborated-type-specifier* ([[dcl.type.elab]]), a class definition
|
| 581 |
+
(Clause [[class]]), a constructor declaration ([[class.ctor]]), or a
|
| 582 |
+
destructor declaration ([[class.dtor]]), the program is
|
| 583 |
+
ill-formed. — *end note*]
|
| 584 |
+
|
| 585 |
+
[*Example 8*:
|
| 586 |
|
| 587 |
``` cpp
|
| 588 |
struct S {
|
| 589 |
S();
|
| 590 |
~S();
|
|
|
|
| 594 |
|
| 595 |
S a = T(); // OK
|
| 596 |
struct T * p; // error
|
| 597 |
```
|
| 598 |
|
| 599 |
+
— *end example*]
|
| 600 |
+
|
| 601 |
If the typedef declaration defines an unnamed class (or enum), the first
|
| 602 |
*typedef-name* declared by the declaration to be that class type (or
|
| 603 |
enum type) is used to denote the class type (or enum type) for linkage
|
| 604 |
purposes only ([[basic.link]]).
|
| 605 |
|
| 606 |
+
[*Example 9*:
|
| 607 |
+
|
| 608 |
``` cpp
|
| 609 |
typedef struct { } *ps, S; // S is the class name for linkage purposes
|
| 610 |
```
|
| 611 |
|
| 612 |
+
— *end example*]
|
| 613 |
+
|
| 614 |
### The `friend` specifier <a id="dcl.friend">[[dcl.friend]]</a>
|
| 615 |
|
| 616 |
The `friend` specifier is used to specify access to class members; see
|
| 617 |
[[class.friend]].
|
| 618 |
|
| 619 |
### The `constexpr` specifier <a id="dcl.constexpr">[[dcl.constexpr]]</a>
|
| 620 |
|
| 621 |
The `constexpr` specifier shall be applied only to the definition of a
|
| 622 |
+
variable or variable template or the declaration of a function or
|
| 623 |
+
function template. A function or static data member declared with the
|
| 624 |
+
`constexpr` specifier is implicitly an inline function or variable (
|
| 625 |
+
[[dcl.inline]]). If any declaration of a function or function template
|
| 626 |
+
has a `constexpr` specifier, then all its declarations shall contain the
|
| 627 |
+
`constexpr` specifier.
|
| 628 |
+
|
| 629 |
+
[*Note 1*: An explicit specialization can differ from the template
|
| 630 |
+
declaration with respect to the `constexpr` specifier. — *end note*]
|
| 631 |
+
|
| 632 |
+
[*Note 2*: Function parameters cannot be declared
|
| 633 |
+
`constexpr`. — *end note*]
|
| 634 |
+
|
| 635 |
+
[*Example 1*:
|
| 636 |
|
| 637 |
``` cpp
|
| 638 |
constexpr void square(int &x); // OK: declaration
|
| 639 |
constexpr int bufsz = 1024; // OK: definition
|
| 640 |
constexpr struct pixel { // error: pixel is a type
|
|
|
|
| 656 |
return x + 1;
|
| 657 |
}
|
| 658 |
extern constexpr int memsz; // error: not a definition
|
| 659 |
```
|
| 660 |
|
| 661 |
+
— *end example*]
|
| 662 |
+
|
| 663 |
A `constexpr` specifier used in the declaration of a function that is
|
| 664 |
not a constructor declares that function to be a *constexpr function*.
|
| 665 |
Similarly, a `constexpr` specifier used in a constructor declaration
|
| 666 |
+
declares that constructor to be a *constexpr constructor*.
|
|
|
|
|
|
|
| 667 |
|
| 668 |
+
The definition of a constexpr function shall satisfy the following
|
| 669 |
+
requirements:
|
| 670 |
|
| 671 |
- it shall not be virtual ([[class.virtual]]);
|
| 672 |
- its return type shall be a literal type;
|
| 673 |
- each of its parameter types shall be a literal type;
|
| 674 |
- its *function-body* shall be `= delete`, `= default`, or a
|
| 675 |
*compound-statement* that does not contain
|
| 676 |
- an *asm-definition*,
|
| 677 |
- a `goto` statement,
|
| 678 |
+
- an identifier label ([[stmt.label]]),
|
| 679 |
- a *try-block*, or
|
| 680 |
- a definition of a variable of non-literal type or of static or
|
| 681 |
thread storage duration or for which no initialization is performed.
|
| 682 |
|
| 683 |
+
[*Example 2*:
|
| 684 |
+
|
| 685 |
``` cpp
|
| 686 |
constexpr int square(int x)
|
| 687 |
{ return x * x; } // OK
|
| 688 |
constexpr long long_max()
|
| 689 |
{ return 2147483647; } // OK
|
|
|
|
| 707 |
while (--n > 0) r *= x;
|
| 708 |
return r;
|
| 709 |
}
|
| 710 |
```
|
| 711 |
|
| 712 |
+
— *end example*]
|
| 713 |
+
|
| 714 |
+
The definition of a constexpr constructor shall satisfy the following
|
| 715 |
+
requirements:
|
| 716 |
|
| 717 |
- the class shall not have any virtual base classes;
|
| 718 |
- each of the parameter types shall be a literal type;
|
| 719 |
+
- its *function-body* shall not be a *function-try-block*.
|
| 720 |
|
| 721 |
In addition, either its *function-body* shall be `= delete`, or it shall
|
| 722 |
+
satisfy the following requirements:
|
| 723 |
|
| 724 |
- either its *function-body* shall be `= default`, or the
|
| 725 |
*compound-statement* of its *function-body* shall satisfy the
|
| 726 |
+
requirements for a *function-body* of a constexpr function;
|
| 727 |
+
- every non-variant non-static data member and base class subobject
|
| 728 |
shall be initialized ([[class.base.init]]);
|
| 729 |
- if the class is a union having variant members ([[class.union]]),
|
| 730 |
exactly one of them shall be initialized;
|
| 731 |
- if the class is a union-like class, but is not a union, for each of
|
| 732 |
its anonymous union members having variant members, exactly one of
|
| 733 |
them shall be initialized;
|
| 734 |
- for a non-delegating constructor, every constructor selected to
|
| 735 |
+
initialize non-static data members and base class subobjects shall be
|
| 736 |
+
a constexpr constructor;
|
| 737 |
- for a delegating constructor, the target constructor shall be a
|
| 738 |
+
constexpr constructor.
|
| 739 |
+
|
| 740 |
+
[*Example 3*:
|
| 741 |
|
| 742 |
``` cpp
|
| 743 |
struct Length {
|
| 744 |
constexpr explicit Length(int i = 0) : val(i) { }
|
| 745 |
private:
|
| 746 |
int val;
|
| 747 |
};
|
| 748 |
```
|
| 749 |
|
| 750 |
+
— *end example*]
|
| 751 |
+
|
| 752 |
+
For a constexpr function or constexpr constructor that is neither
|
| 753 |
+
defaulted nor a template, if no argument values exist such that an
|
| 754 |
+
invocation of the function or constructor could be an evaluated
|
| 755 |
+
subexpression of a core constant expression ([[expr.const]]), or, for a
|
| 756 |
+
constructor, a constant initializer for some object (
|
| 757 |
+
[[basic.start.static]]), the program is ill-formed, no diagnostic
|
| 758 |
required.
|
| 759 |
|
| 760 |
+
[*Example 4*:
|
| 761 |
+
|
| 762 |
``` cpp
|
| 763 |
constexpr int f(bool b)
|
| 764 |
{ return b ? throw 0 : 0; } // OK
|
| 765 |
constexpr int f() { return f(true); } // ill-formed, no diagnostic required
|
| 766 |
|
|
|
|
| 775 |
constexpr D() : B(global) { } // ill-formed, no diagnostic required
|
| 776 |
// lvalue-to-rvalue conversion on non-constant global
|
| 777 |
};
|
| 778 |
```
|
| 779 |
|
| 780 |
+
— *end example*]
|
| 781 |
+
|
| 782 |
+
If the instantiated template specialization of a constexpr function
|
| 783 |
template or member function of a class template would fail to satisfy
|
| 784 |
+
the requirements for a constexpr function or constexpr constructor, that
|
| 785 |
+
specialization is still a constexpr function or constexpr constructor,
|
| 786 |
+
even though a call to such a function cannot appear in a constant
|
| 787 |
+
expression. If no specialization of the template would satisfy the
|
| 788 |
+
requirements for a constexpr function or constexpr constructor when
|
| 789 |
+
considered as a non-template function or constructor, the template is
|
| 790 |
+
ill-formed, no diagnostic required.
|
| 791 |
+
|
| 792 |
+
A call to a constexpr function produces the same result as a call to an
|
| 793 |
+
equivalent non-constexpr function in all respects except that
|
| 794 |
+
|
| 795 |
+
- a call to a constexpr function can appear in a constant expression (
|
| 796 |
+
[[expr.const]]) and
|
| 797 |
+
- copy elision is mandatory in a constant expression ([[class.copy]]).
|
| 798 |
+
|
| 799 |
+
The `constexpr` specifier has no effect on the type of a constexpr
|
| 800 |
+
function or a constexpr constructor.
|
| 801 |
+
|
| 802 |
+
[*Example 5*:
|
| 803 |
|
| 804 |
``` cpp
|
| 805 |
constexpr int bar(int x, int y) // OK
|
| 806 |
{ return x + y + x*y; }
|
| 807 |
// ...
|
| 808 |
int bar(int x, int y) // error: redefinition of bar
|
| 809 |
{ return x * 2 + 3 * y; }
|
| 810 |
```
|
| 811 |
|
| 812 |
+
— *end example*]
|
| 813 |
+
|
| 814 |
A `constexpr` specifier used in an object declaration declares the
|
| 815 |
object as `const`. Such an object shall have literal type and shall be
|
| 816 |
+
initialized. In any `constexpr` variable declaration, the
|
| 817 |
+
full-expression of the initialization shall be a constant expression (
|
| 818 |
+
[[expr.const]]).
|
| 819 |
+
|
| 820 |
+
[*Example 6*:
|
|
|
|
|
|
|
| 821 |
|
| 822 |
``` cpp
|
| 823 |
struct pixel {
|
| 824 |
int x, y;
|
| 825 |
};
|
| 826 |
constexpr pixel ur = { 1294, 1024 }; // OK
|
| 827 |
constexpr pixel origin; // error: initializer missing
|
| 828 |
```
|
| 829 |
|
| 830 |
+
— *end example*]
|
| 831 |
+
|
| 832 |
+
### The `inline` specifier <a id="dcl.inline">[[dcl.inline]]</a>
|
| 833 |
+
|
| 834 |
+
The `inline` specifier can be applied only to the declaration or
|
| 835 |
+
definition of a variable or function.
|
| 836 |
+
|
| 837 |
+
A function declaration ([[dcl.fct]], [[class.mfct]], [[class.friend]])
|
| 838 |
+
with an `inline` specifier declares an *inline function*. The inline
|
| 839 |
+
specifier indicates to the implementation that inline substitution of
|
| 840 |
+
the function body at the point of call is to be preferred to the usual
|
| 841 |
+
function call mechanism. An implementation is not required to perform
|
| 842 |
+
this inline substitution at the point of call; however, even if this
|
| 843 |
+
inline substitution is omitted, the other rules for inline functions
|
| 844 |
+
specified in this section shall still be respected.
|
| 845 |
+
|
| 846 |
+
A variable declaration with an `inline` specifier declares an *inline
|
| 847 |
+
variable*.
|
| 848 |
+
|
| 849 |
+
A function defined within a class definition is an inline function.
|
| 850 |
+
|
| 851 |
+
The `inline` specifier shall not appear on a block scope
|
| 852 |
+
declaration.[^2] If the `inline` specifier is used in a friend function
|
| 853 |
+
declaration, that declaration shall be a definition or the function
|
| 854 |
+
shall have previously been declared inline.
|
| 855 |
+
|
| 856 |
+
An inline function or variable shall be defined in every translation
|
| 857 |
+
unit in which it is odr-used and shall have exactly the same definition
|
| 858 |
+
in every case ([[basic.def.odr]]).
|
| 859 |
+
|
| 860 |
+
[*Note 1*: A call to the inline function or a use of the inline
|
| 861 |
+
variable may be encountered before its definition appears in the
|
| 862 |
+
translation unit. — *end note*]
|
| 863 |
+
|
| 864 |
+
If the definition of a function or variable appears in a translation
|
| 865 |
+
unit before its first declaration as inline, the program is ill-formed.
|
| 866 |
+
If a function or variable with external linkage is declared inline in
|
| 867 |
+
one translation unit, it shall be declared inline in all translation
|
| 868 |
+
units in which it appears; no diagnostic is required. An inline function
|
| 869 |
+
or variable with external linkage shall have the same address in all
|
| 870 |
+
translation units.
|
| 871 |
+
|
| 872 |
+
[*Note 2*: A `static` local variable in an inline function with
|
| 873 |
+
external linkage always refers to the same object. A type defined within
|
| 874 |
+
the body of an inline function with external linkage is the same type in
|
| 875 |
+
every translation unit. — *end note*]
|
| 876 |
+
|
| 877 |
### Type specifiers <a id="dcl.type">[[dcl.type]]</a>
|
| 878 |
|
| 879 |
The type-specifiers are
|
| 880 |
|
| 881 |
``` bnf
|
| 882 |
type-specifier:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 883 |
simple-type-specifier
|
| 884 |
elaborated-type-specifier
|
| 885 |
typename-specifier
|
| 886 |
cv-qualifier
|
| 887 |
```
|
|
|
|
| 891 |
type-specifier attribute-specifier-seqₒₚₜ
|
| 892 |
type-specifier type-specifier-seq
|
| 893 |
```
|
| 894 |
|
| 895 |
``` bnf
|
| 896 |
+
defining-type-specifier:
|
| 897 |
+
type-specifier
|
| 898 |
+
class-specifier
|
| 899 |
+
enum-specifier
|
| 900 |
+
```
|
| 901 |
+
|
| 902 |
+
``` bnf
|
| 903 |
+
defining-type-specifier-seq:
|
| 904 |
+
defining-type-specifier attribute-specifier-seqₒₚₜ
|
| 905 |
+
defining-type-specifier defining-type-specifier-seq
|
| 906 |
```
|
| 907 |
|
| 908 |
The optional *attribute-specifier-seq* in a *type-specifier-seq* or a
|
| 909 |
+
*defining-type-specifier-seq* appertains to the type denoted by the
|
| 910 |
+
preceding *type-specifier*s or *defining-type-specifier*s (
|
| 911 |
+
[[dcl.meaning]]). The *attribute-specifier-seq* affects the type only
|
| 912 |
+
for the declaration it appears in, not other declarations involving the
|
| 913 |
+
same type.
|
| 914 |
|
| 915 |
+
As a general rule, at most one *defining-type-specifier* is allowed in
|
| 916 |
+
the complete *decl-specifier-seq* of a *declaration* or in a
|
| 917 |
+
*defining-type-specifier-seq*, and at most one *type-specifier* is
|
| 918 |
+
allowed in a *type-specifier-seq*. The only exceptions to this rule are
|
| 919 |
+
the following:
|
| 920 |
|
| 921 |
- `const` can be combined with any type specifier except itself.
|
| 922 |
- `volatile` can be combined with any type specifier except itself.
|
| 923 |
- `signed` or `unsigned` can be combined with `char`, `long`, `short`,
|
| 924 |
or `int`.
|
| 925 |
- `short` or `long` can be combined with `int`.
|
| 926 |
- `long` can be combined with `double`.
|
| 927 |
- `long` can be combined with `long`.
|
| 928 |
|
| 929 |
Except in a declaration of a constructor, destructor, or conversion
|
| 930 |
+
function, at least one *defining-type-specifier* that is not a
|
| 931 |
+
*cv-qualifier* shall appear in a complete *type-specifier-seq* or a
|
| 932 |
+
complete *decl-specifier-seq*.[^3]
|
|
|
|
|
|
|
|
|
|
| 933 |
|
| 934 |
+
[*Note 1*: *enum-specifier*s, *class-specifier*s, and
|
| 935 |
+
*typename-specifier*s are discussed in [[dcl.enum]], Clause [[class]],
|
| 936 |
+
and [[temp.res]], respectively. The remaining *type-specifier*s are
|
| 937 |
+
discussed in the rest of this section. — *end note*]
|
| 938 |
|
| 939 |
+
#### The *cv-qualifier*s <a id="dcl.type.cv">[[dcl.type.cv]]</a>
|
| 940 |
|
| 941 |
+
There are two *cv-qualifier*s, `const` and `volatile`. Each
|
| 942 |
*cv-qualifier* shall appear at most once in a *cv-qualifier-seq*. If a
|
| 943 |
*cv-qualifier* appears in a *decl-specifier-seq*, the
|
| 944 |
+
*init-declarator-list* or *member-declarator-list* of the declaration
|
| 945 |
+
shall not be empty.
|
| 946 |
+
|
| 947 |
+
[*Note 1*: [[basic.type.qualifier]] and [[dcl.fct]] describe how
|
| 948 |
+
cv-qualifiers affect object and function types. — *end note*]
|
| 949 |
+
|
| 950 |
+
Redundant cv-qualifications are ignored.
|
| 951 |
+
|
| 952 |
+
[*Note 2*: For example, these could be introduced by
|
| 953 |
+
typedefs. — *end note*]
|
| 954 |
+
|
| 955 |
+
[*Note 3*: Declaring a variable `const` can affect its linkage (
|
| 956 |
+
[[dcl.stc]]) and its usability in constant expressions (
|
| 957 |
+
[[expr.const]]). As described in [[dcl.init]], the definition of an
|
| 958 |
+
object or subobject of const-qualified type must specify an initializer
|
| 959 |
+
or be subject to default-initialization. — *end note*]
|
| 960 |
|
| 961 |
A pointer or reference to a cv-qualified type need not actually point or
|
| 962 |
refer to a cv-qualified object, but it is treated as if it does; a
|
| 963 |
const-qualified access path cannot be used to modify an object even if
|
| 964 |
the object referenced is a non-const object and can be modified through
|
| 965 |
+
some other access path.
|
| 966 |
+
|
| 967 |
+
[*Note 4*: Cv-qualifiers are supported by the type system so that they
|
| 968 |
+
cannot be subverted without casting (
|
| 969 |
+
[[expr.const.cast]]). — *end note*]
|
| 970 |
|
| 971 |
Except that any class member declared `mutable` ([[dcl.stc]]) can be
|
| 972 |
modified, any attempt to modify a `const` object during its lifetime (
|
| 973 |
[[basic.life]]) results in undefined behavior.
|
| 974 |
|
| 975 |
+
[*Example 1*:
|
| 976 |
+
|
| 977 |
``` cpp
|
| 978 |
const int ci = 3; // cv-qualified (initialized as required)
|
| 979 |
ci = 4; // ill-formed: attempt to modify const
|
| 980 |
|
| 981 |
int i = 2; // not cv-qualified
|
|
|
|
| 990 |
const int* ciq = new const int (3); // initialized as required
|
| 991 |
int* iq = const_cast<int*>(ciq); // cast required
|
| 992 |
*iq = 4; // undefined: modifies a const object
|
| 993 |
```
|
| 994 |
|
| 995 |
+
For another example,
|
| 996 |
|
| 997 |
``` cpp
|
| 998 |
struct X {
|
| 999 |
mutable int i;
|
| 1000 |
int j;
|
|
|
|
| 1010 |
Y* p = const_cast<Y*>(&y); // cast away const-ness of y
|
| 1011 |
p->x.i = 99; // well-formed: mutable member can be modified
|
| 1012 |
p->x.j = 99; // undefined: modifies a const member
|
| 1013 |
```
|
| 1014 |
|
| 1015 |
+
— *end example*]
|
|
|
|
|
|
|
|
|
|
| 1016 |
|
| 1017 |
+
The semantics of an access through a volatile glvalue are
|
| 1018 |
+
*implementation-defined*. If an attempt is made to access an object
|
| 1019 |
+
defined with a volatile-qualified type through the use of a non-volatile
|
| 1020 |
+
glvalue, the behavior is undefined.
|
| 1021 |
+
|
| 1022 |
+
[*Note 5*: `volatile` is a hint to the implementation to avoid
|
| 1023 |
+
aggressive optimization involving the object because the value of the
|
| 1024 |
+
object might be changed by means undetectable by an implementation.
|
| 1025 |
+
Furthermore, for some implementations, `volatile` might indicate that
|
| 1026 |
+
special hardware instructions are required to access the object. See
|
| 1027 |
+
[[intro.execution]] for detailed semantics. In general, the semantics of
|
| 1028 |
+
`volatile` are intended to be the same in C++as they are in
|
| 1029 |
+
C. — *end note*]
|
| 1030 |
|
| 1031 |
#### Simple type specifiers <a id="dcl.type.simple">[[dcl.type.simple]]</a>
|
| 1032 |
|
| 1033 |
The simple type specifiers are
|
| 1034 |
|
| 1035 |
``` bnf
|
| 1036 |
simple-type-specifier:
|
| 1037 |
nested-name-specifierₒₚₜ type-name
|
| 1038 |
nested-name-specifier 'template' simple-template-id
|
| 1039 |
+
nested-name-specifierₒₚₜ template-name
|
| 1040 |
'char'
|
| 1041 |
'char16_t'
|
| 1042 |
'char32_t'
|
| 1043 |
'wchar_t'
|
| 1044 |
'bool'
|
|
|
|
| 1066 |
decltype-specifier:
|
| 1067 |
'decltype' '(' expression ')'
|
| 1068 |
'decltype' '(' 'auto' ')'
|
| 1069 |
```
|
| 1070 |
|
| 1071 |
+
The *simple-type-specifier* `auto` is a placeholder for a type to be
|
| 1072 |
+
deduced ([[dcl.spec.auto]]). A *type-specifier* of the form
|
| 1073 |
+
`typename`ₒₚₜ *nested-name-specifier*ₒₚₜ *template-name* is a
|
| 1074 |
+
placeholder for a deduced class type ([[dcl.type.class.deduct]]). The
|
| 1075 |
+
*template-name* shall name a class template that is not an
|
| 1076 |
+
injected-class-name. The other *simple-type-specifier*s specify either a
|
| 1077 |
previously-declared type, a type determined from an expression, or one
|
| 1078 |
of the fundamental types ([[basic.fundamental]]). Table
|
| 1079 |
[[tab:simple.type.specifiers]] summarizes the valid combinations of
|
| 1080 |
*simple-type-specifier*s and the types they specify.
|
| 1081 |
|
| 1082 |
**Table: *simple-type-specifier*{s} and the types they specify** <a id="tab:simple.type.specifiers">[tab:simple.type.specifiers]</a>
|
| 1083 |
|
| 1084 |
+
| Specifier(s) | Type |
|
| 1085 |
| ---------------------- | -------------------------------------- |
|
| 1086 |
| *type-name* | the type named |
|
| 1087 |
| *simple-template-id* | the type as defined in~ [[temp.names]] |
|
| 1088 |
+
| *template-name* | placeholder for a type to be deduced |
|
| 1089 |
| char | ``char'' |
|
| 1090 |
| unsigned char | ``unsigned char'' |
|
| 1091 |
| signed char | ``signed char'' |
|
| 1092 |
| char16_t | ``char16_t'' |
|
| 1093 |
| char32_t | ``char32_t'' |
|
|
|
|
| 1119 |
| float | ``float'' |
|
| 1120 |
| double | ``double'' |
|
| 1121 |
| long double | ``long double'' |
|
| 1122 |
| void | ``void'' |
|
| 1123 |
| auto | placeholder for a type to be deduced |
|
| 1124 |
+
| decltype(auto) | placeholder for a type to be deduced |
|
| 1125 |
| decltype(*expression*) | the type as defined below |
|
| 1126 |
|
| 1127 |
|
| 1128 |
+
When multiple *simple-type-specifier*s are allowed, they can be freely
|
| 1129 |
+
intermixed with other *decl-specifier*s in any order.
|
| 1130 |
+
|
| 1131 |
+
[*Note 1*: It is *implementation-defined* whether objects of `char`
|
| 1132 |
+
type are represented as signed or unsigned quantities. The `signed`
|
| 1133 |
+
specifier forces `char` objects to be signed; it is redundant in other
|
| 1134 |
+
contexts. — *end note*]
|
| 1135 |
|
| 1136 |
For an expression `e`, the type denoted by `decltype(e)` is defined as
|
| 1137 |
follows:
|
| 1138 |
|
| 1139 |
+
- if `e` is an unparenthesized *id-expression* naming a structured
|
| 1140 |
+
binding ([[dcl.struct.bind]]), `decltype(e)` is the referenced type
|
| 1141 |
+
as given in the specification of the structured binding declaration;
|
| 1142 |
+
- otherwise, if `e` is an unparenthesized *id-expression* or an
|
| 1143 |
+
unparenthesized class member access ([[expr.ref]]), `decltype(e)` is
|
| 1144 |
+
the type of the entity named by `e`. If there is no such entity, or if
|
| 1145 |
+
`e` names a set of overloaded functions, the program is ill-formed;
|
| 1146 |
- otherwise, if `e` is an xvalue, `decltype(e)` is `T&&`, where `T` is
|
| 1147 |
the type of `e`;
|
| 1148 |
- otherwise, if `e` is an lvalue, `decltype(e)` is `T&`, where `T` is
|
| 1149 |
the type of `e`;
|
| 1150 |
- otherwise, `decltype(e)` is the type of `e`.
|
| 1151 |
|
| 1152 |
The operand of the `decltype` specifier is an unevaluated operand
|
| 1153 |
(Clause [[expr]]).
|
| 1154 |
|
| 1155 |
+
[*Example 1*:
|
| 1156 |
+
|
| 1157 |
``` cpp
|
| 1158 |
const int&& foo();
|
| 1159 |
int i;
|
| 1160 |
struct A { double x; };
|
| 1161 |
const A* a = new A();
|
| 1162 |
+
decltype(foo()) x1 = 17; // type is const int&&
|
| 1163 |
decltype(i) x2; // type is int
|
| 1164 |
decltype(a->x) x3; // type is double
|
| 1165 |
decltype((a->x)) x4 = x3; // type is const double&
|
| 1166 |
```
|
| 1167 |
|
| 1168 |
+
— *end example*]
|
| 1169 |
+
|
| 1170 |
+
[*Note 2*: The rules for determining types involving `decltype(auto)`
|
| 1171 |
+
are specified in [[dcl.spec.auto]]. — *end note*]
|
| 1172 |
+
|
| 1173 |
+
If the operand of a *decltype-specifier* is a prvalue, the temporary
|
| 1174 |
+
materialization conversion is not applied ([[conv.rval]]) and no result
|
| 1175 |
+
object is provided for the prvalue. The type of the prvalue may be
|
| 1176 |
+
incomplete.
|
| 1177 |
+
|
| 1178 |
+
[*Note 3*: As a result, storage is not allocated for the prvalue and it
|
| 1179 |
+
is not destroyed. Thus, a class type is not instantiated as a result of
|
| 1180 |
+
being the type of a function call in this context. In this context, the
|
| 1181 |
+
common purpose of writing the expression is merely to refer to its type.
|
| 1182 |
+
In that sense, a *decltype-specifier* is analogous to a use of a
|
| 1183 |
+
*typedef-name*, so the usual reasons for requiring a complete type do
|
| 1184 |
+
not apply. In particular, it is not necessary to allocate storage for a
|
| 1185 |
+
temporary object or to enforce the semantic constraints associated with
|
| 1186 |
+
invoking the type’s destructor. — *end note*]
|
| 1187 |
+
|
| 1188 |
+
[*Note 4*: Unlike the preceding rule, parentheses have no special
|
| 1189 |
+
meaning in this context. — *end note*]
|
| 1190 |
+
|
| 1191 |
+
[*Example 2*:
|
| 1192 |
|
| 1193 |
``` cpp
|
| 1194 |
template<class T> struct A { ~A() = delete; };
|
| 1195 |
template<class T> auto h()
|
| 1196 |
-> A<T>;
|
| 1197 |
template<class T> auto i(T) // identity
|
| 1198 |
-> T;
|
| 1199 |
template<class T> auto f(T) // #1
|
| 1200 |
+
-> decltype(i(h<T>())); // forces completion of A<T> and implicitly uses A<T>::~A()
|
| 1201 |
+
// for the temporary introduced by the use of h().
|
| 1202 |
+
// (A temporary is not introduced as a result of the use of i().)
|
|
|
|
| 1203 |
template<class T> auto f(T) // #2
|
| 1204 |
-> void;
|
| 1205 |
auto g() -> void {
|
| 1206 |
+
f(42); // OK: calls #2. (#1 is not a viable candidate: type deduction
|
| 1207 |
+
// fails~([temp.deduct]) because A<int>::~{A()} is implicitly used in its
|
| 1208 |
+
// decltype-specifier)
|
| 1209 |
}
|
| 1210 |
template<class T> auto q(T)
|
| 1211 |
+
-> decltype((h<T>())); // does not force completion of A<T>; A<T>::~A() is not implicitly
|
| 1212 |
+
// used within the context of this decltype-specifier
|
| 1213 |
void r() {
|
| 1214 |
+
q(42); // Error: deduction against q succeeds, so overload resolution selects
|
| 1215 |
+
// the specialization ``q(T) -> decltype((h<T>())) [with T=int]''.
|
| 1216 |
// The return type is A<int>, so a temporary is introduced and its
|
| 1217 |
// destructor is used, so the program is ill-formed.
|
| 1218 |
}
|
| 1219 |
```
|
| 1220 |
|
| 1221 |
+
— *end example*]
|
| 1222 |
+
|
| 1223 |
#### Elaborated type specifiers <a id="dcl.type.elab">[[dcl.type.elab]]</a>
|
| 1224 |
|
| 1225 |
``` bnf
|
| 1226 |
elaborated-type-specifier:
|
| 1227 |
class-key attribute-specifier-seqₒₚₜ nested-name-specifierₒₚₜ identifier
|
|
|
|
| 1255 |
resolves to a *class-name* or *enum-name*, the
|
| 1256 |
*elaborated-type-specifier* introduces it into the declaration the same
|
| 1257 |
way a *simple-type-specifier* introduces its *type-name*. If the
|
| 1258 |
*identifier* resolves to a *typedef-name* or the *simple-template-id*
|
| 1259 |
resolves to an alias template specialization, the
|
| 1260 |
+
*elaborated-type-specifier* is ill-formed.
|
| 1261 |
+
|
| 1262 |
+
[*Note 1*:
|
| 1263 |
+
|
| 1264 |
+
This implies that, within a class template with a template
|
| 1265 |
+
*type-parameter* `T`, the declaration
|
| 1266 |
|
| 1267 |
``` cpp
|
| 1268 |
friend class T;
|
| 1269 |
```
|
| 1270 |
|
| 1271 |
is ill-formed. However, the similar declaration `friend T;` is allowed (
|
| 1272 |
[[class.friend]]).
|
| 1273 |
|
| 1274 |
+
— *end note*]
|
| 1275 |
+
|
| 1276 |
The *class-key* or `enum` keyword present in the
|
| 1277 |
*elaborated-type-specifier* shall agree in kind with the declaration to
|
| 1278 |
which the name in the *elaborated-type-specifier* refers. This rule also
|
| 1279 |
applies to the form of *elaborated-type-specifier* that declares a
|
| 1280 |
*class-name* or `friend` class since it can be construed as referring to
|
|
|
|
| 1283 |
[[dcl.enum]]), the `union` *class-key* shall be used to refer to a union
|
| 1284 |
(Clause [[class]]), and either the `class` or `struct` *class-key*
|
| 1285 |
shall be used to refer to a class (Clause [[class]]) declared using the
|
| 1286 |
`class` or `struct` *class-key*.
|
| 1287 |
|
| 1288 |
+
[*Example 1*:
|
| 1289 |
+
|
| 1290 |
``` cpp
|
| 1291 |
enum class E { a, b };
|
| 1292 |
enum E x = E::a; // OK
|
| 1293 |
```
|
| 1294 |
|
| 1295 |
+
— *end example*]
|
| 1296 |
|
| 1297 |
+
#### The `auto` specifier <a id="dcl.spec.auto">[[dcl.spec.auto]]</a>
|
| 1298 |
+
|
| 1299 |
+
The `auto` and `decltype(auto)` *type-specifier*s are used to designate
|
| 1300 |
+
a placeholder type that will be replaced later by deduction from an
|
| 1301 |
+
initializer. The `auto` *type-specifier* is also used to introduce a
|
| 1302 |
+
function type having a *trailing-return-type* or to signify that a
|
| 1303 |
+
lambda is a generic lambda ([[expr.prim.lambda.closure]]). The `auto`
|
| 1304 |
+
*type-specifier* is also used to introduce a structured binding
|
| 1305 |
+
declaration ([[dcl.struct.bind]]).
|
| 1306 |
|
| 1307 |
The placeholder type can appear with a function declarator in the
|
| 1308 |
*decl-specifier-seq*, *type-specifier-seq*, *conversion-function-id*, or
|
| 1309 |
*trailing-return-type*, in any context where such a declarator is valid.
|
| 1310 |
If the function declarator includes a *trailing-return-type* (
|
| 1311 |
+
[[dcl.fct]]), that *trailing-return-type* specifies the declared return
|
| 1312 |
+
type of the function. Otherwise, the function declarator shall declare a
|
| 1313 |
+
function. If the declared return type of the function contains a
|
| 1314 |
+
placeholder type, the return type of the function is deduced from
|
| 1315 |
+
non-discarded `return` statements, if any, in the body of the function (
|
| 1316 |
+
[[stmt.if]]).
|
| 1317 |
|
| 1318 |
If the `auto` *type-specifier* appears as one of the *decl-specifier*s
|
| 1319 |
in the *decl-specifier-seq* of a *parameter-declaration* of a
|
| 1320 |
*lambda-expression*, the lambda is a *generic lambda* (
|
| 1321 |
+
[[expr.prim.lambda.closure]]).
|
| 1322 |
+
|
| 1323 |
+
[*Example 1*:
|
| 1324 |
|
| 1325 |
``` cpp
|
| 1326 |
auto glambda = [](int i, auto a) { return i; }; // OK: a generic lambda
|
| 1327 |
```
|
| 1328 |
|
| 1329 |
+
— *end example*]
|
| 1330 |
+
|
| 1331 |
The type of a variable declared using `auto` or `decltype(auto)` is
|
| 1332 |
+
deduced from its initializer. This use is allowed in an initializing
|
| 1333 |
+
declaration ([[dcl.init]]) of a variable. `auto` or `decltype(auto)`
|
| 1334 |
+
shall appear as one of the *decl-specifier*s in the *decl-specifier-seq*
|
| 1335 |
+
and the *decl-specifier-seq* shall be followed by one or more
|
| 1336 |
+
*declarator*s, each of which shall be followed by a non-empty
|
|
|
|
| 1337 |
*initializer*. In an *initializer* of the form
|
| 1338 |
|
| 1339 |
``` cpp
|
| 1340 |
( expression-list )
|
| 1341 |
```
|
| 1342 |
|
| 1343 |
the *expression-list* shall be a single *assignment-expression*.
|
| 1344 |
|
| 1345 |
+
[*Example 2*:
|
| 1346 |
+
|
| 1347 |
``` cpp
|
| 1348 |
auto x = 5; // OK: x has type int
|
| 1349 |
const auto *v = &x, u = 6; // OK: v has type const int*, u has type const int
|
| 1350 |
static auto y = 0.0; // OK: y has type double
|
| 1351 |
auto int r; // error: auto is not a storage-class-specifier
|
| 1352 |
auto f() -> int; // OK: f returns int
|
| 1353 |
auto g() { return 0.0; } // OK: g returns double
|
| 1354 |
auto h(); // OK: h's return type will be deduced when it is defined
|
| 1355 |
```
|
| 1356 |
|
| 1357 |
+
— *end example*]
|
| 1358 |
+
|
| 1359 |
+
A placeholder type can also be used in the *type-specifier-seq* in the
|
| 1360 |
+
*new-type-id* or *type-id* of a *new-expression* ([[expr.new]]) and as
|
| 1361 |
+
a *decl-specifier* of the *parameter-declaration*'s *decl-specifier-seq*
|
| 1362 |
+
in a *template-parameter* ([[temp.param]]).
|
| 1363 |
|
| 1364 |
A program that uses `auto` or `decltype(auto)` in a context not
|
| 1365 |
explicitly allowed in this section is ill-formed.
|
| 1366 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1367 |
If the *init-declarator-list* contains more than one *init-declarator*,
|
| 1368 |
they shall all form declarations of variables. The type of each declared
|
| 1369 |
+
variable is determined by placeholder type deduction (
|
| 1370 |
+
[[dcl.type.auto.deduct]]), and if the type that replaces the placeholder
|
| 1371 |
+
type is not the same in each deduction, the program is ill-formed.
|
| 1372 |
+
|
| 1373 |
+
[*Example 3*:
|
| 1374 |
|
| 1375 |
``` cpp
|
| 1376 |
auto x = 5, *y = &x; // OK: auto is int
|
| 1377 |
auto a = 5, b = { 1, 2 }; // error: different types for auto
|
| 1378 |
```
|
| 1379 |
|
| 1380 |
+
— *end example*]
|
| 1381 |
+
|
| 1382 |
If a function with a declared return type that contains a placeholder
|
| 1383 |
+
type has multiple non-discarded `return` statements, the return type is
|
| 1384 |
+
deduced for each such `return` statement. If the type deduced is not the
|
| 1385 |
+
same in each deduction, the program is ill-formed.
|
| 1386 |
|
| 1387 |
If a function with a declared return type that uses a placeholder type
|
| 1388 |
+
has no non-discarded `return` statements, the return type is deduced as
|
| 1389 |
+
though from a `return` statement with no operand at the closing brace of
|
| 1390 |
+
the function body.
|
| 1391 |
+
|
| 1392 |
+
[*Example 4*:
|
| 1393 |
|
| 1394 |
``` cpp
|
| 1395 |
auto f() { } // OK, return type is void
|
| 1396 |
auto* g() { } // error, cannot deduce auto* from void()
|
| 1397 |
```
|
| 1398 |
|
| 1399 |
+
— *end example*]
|
| 1400 |
+
|
| 1401 |
If the type of an entity with an undeduced placeholder type is needed to
|
| 1402 |
determine the type of an expression, the program is ill-formed. Once a
|
| 1403 |
+
non-discarded `return` statement has been seen in a function, however,
|
| 1404 |
+
the return type deduced from that statement can be used in the rest of
|
| 1405 |
+
the function, including in other `return` statements.
|
| 1406 |
+
|
| 1407 |
+
[*Example 5*:
|
| 1408 |
|
| 1409 |
``` cpp
|
| 1410 |
auto n = n; // error, n's type is unknown
|
| 1411 |
auto f();
|
| 1412 |
void g() { &f; } // error, f's return type is unknown
|
|
|
|
| 1416 |
else
|
| 1417 |
return sum(i-1)+i; // OK, sum's return type has been deduced
|
| 1418 |
}
|
| 1419 |
```
|
| 1420 |
|
| 1421 |
+
— *end example*]
|
| 1422 |
+
|
| 1423 |
Return type deduction for a function template with a placeholder in its
|
| 1424 |
declared type occurs when the definition is instantiated even if the
|
| 1425 |
function body contains a `return` statement with a non-type-dependent
|
| 1426 |
+
operand.
|
| 1427 |
+
|
| 1428 |
+
[*Note 1*: Therefore, any use of a specialization of the function
|
| 1429 |
+
template will cause an implicit instantiation. Any errors that arise
|
| 1430 |
+
from this instantiation are not in the immediate context of the function
|
| 1431 |
+
type and can result in the program being ill-formed (
|
| 1432 |
+
[[temp.deduct]]). — *end note*]
|
| 1433 |
+
|
| 1434 |
+
[*Example 6*:
|
| 1435 |
|
| 1436 |
``` cpp
|
| 1437 |
template <class T> auto f(T t) { return t; } // return type deduced at instantiation time
|
| 1438 |
typedef decltype(f(1)) fint_t; // instantiates f<int> to deduce return type
|
| 1439 |
template<class T> auto f(T* t) { return *t; }
|
| 1440 |
void g() { int (*p)(int*) = &f; } // instantiates both fs to determine return types,
|
| 1441 |
// chooses second
|
| 1442 |
```
|
| 1443 |
|
| 1444 |
+
— *end example*]
|
| 1445 |
+
|
| 1446 |
Redeclarations or specializations of a function or function template
|
| 1447 |
with a declared return type that uses a placeholder type shall also use
|
| 1448 |
that placeholder, not a deduced type.
|
| 1449 |
|
| 1450 |
+
[*Example 7*:
|
| 1451 |
+
|
| 1452 |
``` cpp
|
| 1453 |
auto f();
|
| 1454 |
auto f() { return 42; } // return type is int
|
| 1455 |
auto f(); // OK
|
| 1456 |
int f(); // error, cannot be overloaded with auto f()
|
|
|
|
| 1471 |
friend T frf(T);
|
| 1472 |
};
|
| 1473 |
auto frf(int i) { return i; } // not a friend of A<int>
|
| 1474 |
```
|
| 1475 |
|
| 1476 |
+
— *end example*]
|
| 1477 |
+
|
| 1478 |
A function declared with a return type that uses a placeholder type
|
| 1479 |
shall not be `virtual` ([[class.virtual]]).
|
| 1480 |
|
| 1481 |
An explicit instantiation declaration ([[temp.explicit]]) does not
|
| 1482 |
cause the instantiation of an entity declared using a placeholder type,
|
| 1483 |
but it also does not prevent that entity from being instantiated as
|
| 1484 |
needed to determine its type.
|
| 1485 |
|
| 1486 |
+
[*Example 8*:
|
| 1487 |
+
|
| 1488 |
``` cpp
|
| 1489 |
template <typename T> auto f(T t) { return t; }
|
| 1490 |
extern template auto f(int); // does not instantiate f<int>
|
| 1491 |
int (*p)(int) = f; // instantiates f<int> to determine its return type, but an explicit
|
| 1492 |
// instantiation definition is still required somewhere in the program
|
| 1493 |
```
|
| 1494 |
|
| 1495 |
+
— *end example*]
|
| 1496 |
+
|
| 1497 |
+
##### Placeholder type deduction <a id="dcl.type.auto.deduct">[[dcl.type.auto.deduct]]</a>
|
| 1498 |
+
|
| 1499 |
+
*Placeholder type deduction* is the process by which a type containing a
|
| 1500 |
+
placeholder type is replaced by a deduced type.
|
| 1501 |
+
|
| 1502 |
+
A type `T` containing a placeholder type, and a corresponding
|
| 1503 |
+
initializer `e`, are determined as follows:
|
| 1504 |
+
|
| 1505 |
+
- for a non-discarded `return` statement that occurs in a function
|
| 1506 |
+
declared with a return type that contains a placeholder type, `T` is
|
| 1507 |
+
the declared return type and `e` is the operand of the `return`
|
| 1508 |
+
statement. If the `return` statement has no operand, then `e` is
|
| 1509 |
+
`void()`;
|
| 1510 |
+
- for a variable declared with a type that contains a placeholder type,
|
| 1511 |
+
`T` is the declared type of the variable and `e` is the initializer.
|
| 1512 |
+
If the initialization is direct-list-initialization, the initializer
|
| 1513 |
+
shall be a *braced-init-list* containing only a single
|
| 1514 |
+
*assignment-expression* and `e` is the *assignment-expression*;
|
| 1515 |
+
- for a non-type template parameter declared with a type that contains a
|
| 1516 |
+
placeholder type, `T` is the declared type of the non-type template
|
| 1517 |
+
parameter and `e` is the corresponding template argument.
|
| 1518 |
+
|
| 1519 |
+
In the case of a `return` statement with no operand or with an operand
|
| 1520 |
+
of type `void`, `T` shall be either `decltype(auto)` or cv `auto`.
|
| 1521 |
+
|
| 1522 |
+
If the deduction is for a `return` statement and `e` is a
|
| 1523 |
+
*braced-init-list* ([[dcl.init.list]]), the program is ill-formed.
|
| 1524 |
+
|
| 1525 |
+
If the placeholder is the `auto` *type-specifier*, the deduced type T'
|
| 1526 |
+
replacing `T` is determined using the rules for template argument
|
| 1527 |
+
deduction. Obtain `P` from `T` by replacing the occurrences of `auto`
|
| 1528 |
+
with either a new invented type template parameter `U` or, if the
|
| 1529 |
+
initialization is copy-list-initialization, with
|
| 1530 |
+
`std::initializer_list<U>`. Deduce a value for `U` using the rules of
|
| 1531 |
+
template argument deduction from a function call (
|
| 1532 |
+
[[temp.deduct.call]]), where `P` is a function template parameter type
|
| 1533 |
+
and the corresponding argument is `e`. If the deduction fails, the
|
| 1534 |
+
declaration is ill-formed. Otherwise, T' is obtained by substituting the
|
| 1535 |
+
deduced `U` into `P`.
|
| 1536 |
+
|
| 1537 |
+
[*Example 9*:
|
| 1538 |
+
|
| 1539 |
+
``` cpp
|
| 1540 |
+
auto x1 = { 1, 2 }; // decltype(x1) is std::initializer_list<int>
|
| 1541 |
+
auto x2 = { 1, 2.0 }; // error: cannot deduce element type
|
| 1542 |
+
auto x3{ 1, 2 }; // error: not a single element
|
| 1543 |
+
auto x4 = { 3 }; // decltype(x4) is std::initializer_list<int>
|
| 1544 |
+
auto x5{ 3 }; // decltype(x5) is int
|
| 1545 |
+
```
|
| 1546 |
+
|
| 1547 |
+
— *end example*]
|
| 1548 |
+
|
| 1549 |
+
[*Example 10*:
|
| 1550 |
+
|
| 1551 |
+
``` cpp
|
| 1552 |
+
const auto &i = expr;
|
| 1553 |
+
```
|
| 1554 |
+
|
| 1555 |
+
The type of `i` is the deduced type of the parameter `u` in the call
|
| 1556 |
+
`f(expr)` of the following invented function template:
|
| 1557 |
+
|
| 1558 |
+
``` cpp
|
| 1559 |
+
template <class U> void f(const U& u);
|
| 1560 |
+
```
|
| 1561 |
+
|
| 1562 |
+
— *end example*]
|
| 1563 |
+
|
| 1564 |
+
If the placeholder is the `decltype(auto)` *type-specifier*, `T` shall
|
| 1565 |
+
be the placeholder alone. The type deduced for `T` is determined as
|
| 1566 |
+
described in [[dcl.type.simple]], as though `e` had been the operand of
|
| 1567 |
+
the `decltype`.
|
| 1568 |
+
|
| 1569 |
+
[*Example 11*:
|
| 1570 |
+
|
| 1571 |
+
``` cpp
|
| 1572 |
+
int i;
|
| 1573 |
+
int&& f();
|
| 1574 |
+
auto x2a(i); // decltype(x2a) is int
|
| 1575 |
+
decltype(auto) x2d(i); // decltype(x2d) is int
|
| 1576 |
+
auto x3a = i; // decltype(x3a) is int
|
| 1577 |
+
decltype(auto) x3d = i; // decltype(x3d) is int
|
| 1578 |
+
auto x4a = (i); // decltype(x4a) is int
|
| 1579 |
+
decltype(auto) x4d = (i); // decltype(x4d) is int&
|
| 1580 |
+
auto x5a = f(); // decltype(x5a) is int
|
| 1581 |
+
decltype(auto) x5d = f(); // decltype(x5d) is int&&
|
| 1582 |
+
auto x6a = { 1, 2 }; // decltype(x6a) is std::initializer_list<int>
|
| 1583 |
+
decltype(auto) x6d = { 1, 2 }; // error, { 1, 2 } is not an expression
|
| 1584 |
+
auto *x7a = &i; // decltype(x7a) is int*
|
| 1585 |
+
decltype(auto)*x7d = &i; // error, declared type is not plain decltype(auto)
|
| 1586 |
+
```
|
| 1587 |
+
|
| 1588 |
+
— *end example*]
|
| 1589 |
+
|
| 1590 |
+
#### Deduced class template specialization types <a id="dcl.type.class.deduct">[[dcl.type.class.deduct]]</a>
|
| 1591 |
+
|
| 1592 |
+
If a placeholder for a deduced class type appears as a *decl-specifier*
|
| 1593 |
+
in the *decl-specifier-seq* of an initializing declaration (
|
| 1594 |
+
[[dcl.init]]) of a variable, the placeholder is replaced by the return
|
| 1595 |
+
type of the function selected by overload resolution for class template
|
| 1596 |
+
deduction ([[over.match.class.deduct]]). If the *decl-specifier-seq* is
|
| 1597 |
+
followed by an *init-declarator-list* or *member-declarator-list*
|
| 1598 |
+
containing more than one *declarator*, the type that replaces the
|
| 1599 |
+
placeholder shall be the same in each deduction.
|
| 1600 |
+
|
| 1601 |
+
A placeholder for a deduced class type can also be used in the
|
| 1602 |
+
*type-specifier-seq* in the *new-type-id* or *type-id* of a
|
| 1603 |
+
*new-expression* ([[expr.new]]), or as the *simple-type-specifier* in
|
| 1604 |
+
an explicit type conversion (functional notation) ([[expr.type.conv]]).
|
| 1605 |
+
A placeholder for a deduced class type shall not appear in any other
|
| 1606 |
+
context.
|
| 1607 |
+
|
| 1608 |
+
[*Example 1*:
|
| 1609 |
+
|
| 1610 |
+
``` cpp
|
| 1611 |
+
template<class T> struct container {
|
| 1612 |
+
container(T t) {}
|
| 1613 |
+
template<class Iter> container(Iter beg, Iter end);
|
| 1614 |
+
};
|
| 1615 |
+
template<class Iter>
|
| 1616 |
+
container(Iter b, Iter e) -> container<typename std::iterator_traits<Iter>::value_type>;
|
| 1617 |
+
std::vector<double> v = { ... };
|
| 1618 |
+
|
| 1619 |
+
container c(7); // OK, deduces int for T
|
| 1620 |
+
auto d = container(v.begin(), v.end()); // OK, deduces double for T
|
| 1621 |
+
container e{5, 6}; // error, int is not an iterator
|
| 1622 |
+
```
|
| 1623 |
+
|
| 1624 |
+
— *end example*]
|
| 1625 |
+
|
| 1626 |
## Enumeration declarations <a id="dcl.enum">[[dcl.enum]]</a>
|
| 1627 |
|
| 1628 |
An enumeration is a distinct type ([[basic.compound]]) with named
|
| 1629 |
+
constants. Its name becomes an *enum-name* within its scope.
|
| 1630 |
|
| 1631 |
``` bnf
|
| 1632 |
enum-name:
|
| 1633 |
identifier
|
| 1634 |
```
|
|
|
|
| 1639 |
enum-head '{' enumerator-list ', }'
|
| 1640 |
```
|
| 1641 |
|
| 1642 |
``` bnf
|
| 1643 |
enum-head:
|
| 1644 |
+
enum-key attribute-specifier-seqₒₚₜ enum-head-nameₒₚₜ enum-baseₒₚₜ
|
| 1645 |
+
```
|
| 1646 |
+
|
| 1647 |
+
``` bnf
|
| 1648 |
+
enum-head-name:
|
| 1649 |
+
nested-name-specifierₒₚₜ identifier
|
| 1650 |
```
|
| 1651 |
|
| 1652 |
``` bnf
|
| 1653 |
opaque-enum-declaration:
|
| 1654 |
+
enum-key attribute-specifier-seqₒₚₜ nested-name-specifierₒₚₜ identifier enum-baseₒₚₜ ';'
|
| 1655 |
```
|
| 1656 |
|
| 1657 |
``` bnf
|
| 1658 |
enum-key:
|
| 1659 |
'enum'
|
|
|
|
| 1678 |
enumerator '=' constant-expression
|
| 1679 |
```
|
| 1680 |
|
| 1681 |
``` bnf
|
| 1682 |
enumerator:
|
| 1683 |
+
identifier attribute-specifier-seqₒₚₜ
|
| 1684 |
```
|
| 1685 |
|
| 1686 |
The optional *attribute-specifier-seq* in the *enum-head* and the
|
| 1687 |
*opaque-enum-declaration* appertains to the enumeration; the attributes
|
| 1688 |
in that *attribute-specifier-seq* are thereafter considered attributes
|
| 1689 |
of the enumeration whenever it is named. A `:` following “`enum`
|
| 1690 |
+
*nested-name-specifier*ₒₚₜ *identifier*” within the
|
| 1691 |
+
*decl-specifier-seq* of a *member-declaration* is parsed as part of an
|
| 1692 |
+
*enum-base*.
|
| 1693 |
+
|
| 1694 |
+
[*Note 1*:
|
| 1695 |
+
|
| 1696 |
+
This resolves a potential ambiguity between the declaration of an
|
| 1697 |
+
enumeration with an *enum-base* and the declaration of an unnamed
|
| 1698 |
+
bit-field of enumeration type.
|
| 1699 |
+
|
| 1700 |
+
[*Example 1*:
|
| 1701 |
|
| 1702 |
``` cpp
|
| 1703 |
struct S {
|
| 1704 |
enum E : int {};
|
| 1705 |
enum E : int {}; // error: redeclaration of enumeration
|
| 1706 |
};
|
| 1707 |
```
|
| 1708 |
|
| 1709 |
+
— *end example*]
|
| 1710 |
+
|
| 1711 |
+
— *end note*]
|
| 1712 |
+
|
| 1713 |
+
If an *opaque-enum-declaration* contains a *nested-name-specifier*, the
|
| 1714 |
+
declaration shall be an explicit specialization ([[temp.expl.spec]]).
|
| 1715 |
+
|
| 1716 |
The enumeration type declared with an *enum-key* of only `enum` is an
|
| 1717 |
+
*unscoped enumeration*, and its *enumerator*s are *unscoped
|
| 1718 |
+
enumerators*. The *enum-key*s `enum class` and `enum struct` are
|
| 1719 |
+
semantically equivalent; an enumeration type declared with one of these
|
| 1720 |
+
is a *scoped enumeration*, and its *enumerator*s are *scoped
|
| 1721 |
+
enumerators*. The optional *identifier* shall not be omitted in the
|
| 1722 |
+
declaration of a scoped enumeration. The *type-specifier-seq* of an
|
| 1723 |
+
*enum-base* shall name an integral type; any cv-qualification is
|
| 1724 |
+
ignored. An *opaque-enum-declaration* declaring an unscoped enumeration
|
| 1725 |
+
shall not omit the *enum-base*. The identifiers in an *enumerator-list*
|
| 1726 |
+
are declared as constants, and can appear wherever constants are
|
| 1727 |
+
required. An *enumerator-definition* with `=` gives the associated
|
| 1728 |
+
*enumerator* the value indicated by the *constant-expression*. If the
|
| 1729 |
+
first *enumerator* has no *initializer*, the value of the corresponding
|
| 1730 |
constant is zero. An *enumerator-definition* without an *initializer*
|
| 1731 |
gives the *enumerator* the value obtained by increasing the value of the
|
| 1732 |
previous *enumerator* by one.
|
| 1733 |
|
| 1734 |
+
[*Example 2*:
|
| 1735 |
+
|
| 1736 |
``` cpp
|
| 1737 |
enum { a, b, c=0 };
|
| 1738 |
enum { d, e, f=e+2 };
|
| 1739 |
```
|
| 1740 |
|
| 1741 |
defines `a`, `c`, and `d` to be zero, `b` and `e` to be `1`, and `f` to
|
| 1742 |
be `3`.
|
| 1743 |
|
| 1744 |
+
— *end example*]
|
| 1745 |
+
|
| 1746 |
+
The optional *attribute-specifier-seq* in an *enumerator* appertains to
|
| 1747 |
+
that enumerator.
|
| 1748 |
+
|
| 1749 |
An *opaque-enum-declaration* is either a redeclaration of an enumeration
|
| 1750 |
+
in the current scope or a declaration of a new enumeration.
|
| 1751 |
+
|
| 1752 |
+
[*Note 2*: An enumeration declared by an *opaque-enum-declaration* has
|
| 1753 |
+
fixed underlying type and is a complete type. The list of enumerators
|
| 1754 |
+
can be provided in a later redeclaration with an
|
| 1755 |
+
*enum-specifier*. — *end note*]
|
| 1756 |
+
|
| 1757 |
+
A scoped enumeration shall not be later redeclared as unscoped or with a
|
| 1758 |
different underlying type. An unscoped enumeration shall not be later
|
| 1759 |
redeclared as scoped and each redeclaration shall include an *enum-base*
|
| 1760 |
specifying the same underlying type as in the original declaration.
|
| 1761 |
|
| 1762 |
If the *enum-key* is followed by a *nested-name-specifier*, the
|
|
|
|
| 1765 |
*nested-name-specifier* refers (i.e., neither inherited nor introduced
|
| 1766 |
by a *using-declaration*), and the *enum-specifier* shall appear in a
|
| 1767 |
namespace enclosing the previous declaration.
|
| 1768 |
|
| 1769 |
Each enumeration defines a type that is different from all other types.
|
| 1770 |
+
Each enumeration also has an *underlying type*. The underlying type can
|
| 1771 |
+
be explicitly specified using an *enum-base*. For a scoped enumeration
|
| 1772 |
type, the underlying type is `int` if it is not explicitly specified. In
|
| 1773 |
both of these cases, the underlying type is said to be *fixed*.
|
| 1774 |
Following the closing brace of an *enum-specifier*, each enumerator has
|
| 1775 |
the type of its enumeration. If the underlying type is fixed, the type
|
| 1776 |
of each enumerator prior to the closing brace is the underlying type and
|
|
|
|
| 1810 |
is as if the enumeration had a single enumerator with value 0.
|
| 1811 |
|
| 1812 |
For an enumeration whose underlying type is fixed, the values of the
|
| 1813 |
enumeration are the values of the underlying type. Otherwise, for an
|
| 1814 |
enumeration where eₘin is the smallest enumerator and eₘax is the
|
| 1815 |
+
largest, the values of the enumeration are the values in the range bₘin
|
| 1816 |
+
to bₘax, defined as follows: Let K be 1 for a two’s complement
|
| 1817 |
+
representation and 0 for a ones’ complement or sign-magnitude
|
| 1818 |
+
representation. bₘax is the smallest value greater than or equal to
|
| 1819 |
+
max(|eₘin| - K, |eₘax|) and equal to $2^M-1$, where M is a non-negative
|
| 1820 |
+
integer. bₘin is zero if eₘin is non-negative and -(bₘax+K) otherwise.
|
| 1821 |
The size of the smallest bit-field large enough to hold all the values
|
| 1822 |
+
of the enumeration type is max(M,1) if bₘin is zero and M+1 otherwise.
|
| 1823 |
It is possible to define an enumeration that has values not defined by
|
| 1824 |
any of its enumerators. If the *enumerator-list* is empty, the values of
|
| 1825 |
the enumeration are as if the enumeration had a single enumerator with
|
| 1826 |
value 0.[^4]
|
| 1827 |
|
| 1828 |
+
Two enumeration types are *layout-compatible enumerations* if they have
|
| 1829 |
+
the same underlying type.
|
| 1830 |
|
| 1831 |
The value of an enumerator or an object of an unscoped enumeration type
|
| 1832 |
is converted to an integer by integral promotion ([[conv.prom]]).
|
| 1833 |
|
| 1834 |
+
[*Example 3*:
|
| 1835 |
+
|
| 1836 |
``` cpp
|
| 1837 |
enum color { red, yellow, green=20, blue };
|
| 1838 |
color col = red;
|
| 1839 |
color* cp = &col;
|
| 1840 |
if (*cp == blue) // ...
|
|
|
|
| 1846 |
`yellow`, `green`, `blue`; these values can be converted to the integral
|
| 1847 |
values `0`, `1`, `20`, and `21`. Since enumerations are distinct types,
|
| 1848 |
objects of type `color` can be assigned only values of type `color`.
|
| 1849 |
|
| 1850 |
``` cpp
|
| 1851 |
+
color c = 1; // error: type mismatch, no conversion from int to color
|
| 1852 |
+
int i = yellow; // OK: yellow converted to integral value 1, integral promotion
|
|
|
|
|
|
|
|
|
|
| 1853 |
```
|
| 1854 |
|
| 1855 |
Note that this implicit `enum` to `int` conversion is not provided for a
|
| 1856 |
scoped enumeration:
|
| 1857 |
|
|
|
|
| 1860 |
int x = Col::red; // error: no Col to int conversion
|
| 1861 |
Col y = Col::red;
|
| 1862 |
if (y) { } // error: no Col to bool conversion
|
| 1863 |
```
|
| 1864 |
|
| 1865 |
+
— *end example*]
|
| 1866 |
+
|
| 1867 |
Each *enum-name* and each unscoped *enumerator* is declared in the scope
|
| 1868 |
that immediately contains the *enum-specifier*. Each scoped *enumerator*
|
| 1869 |
is declared in the scope of the enumeration. These names obey the scope
|
| 1870 |
+
rules defined for all names in [[basic.scope]] and [[basic.lookup]].
|
| 1871 |
+
|
| 1872 |
+
[*Example 4*:
|
| 1873 |
|
| 1874 |
``` cpp
|
| 1875 |
enum direction { left='l', right='r' };
|
| 1876 |
|
| 1877 |
void g() {
|
|
|
|
| 1887 |
a = high; // error: high not in scope
|
| 1888 |
a = altitude::low; // OK
|
| 1889 |
}
|
| 1890 |
```
|
| 1891 |
|
| 1892 |
+
— *end example*]
|
| 1893 |
+
|
| 1894 |
An enumerator declared in class scope can be referred to using the class
|
| 1895 |
member access operators (`::`, `.` (dot) and `->` (arrow)), see
|
| 1896 |
[[expr.ref]].
|
| 1897 |
|
| 1898 |
+
[*Example 5*:
|
| 1899 |
+
|
| 1900 |
``` cpp
|
| 1901 |
struct X {
|
| 1902 |
enum direction { left='l', right='r' };
|
| 1903 |
int f(int i) { return i==left ? 0 : i==right ? 1 : 2; }
|
| 1904 |
};
|
|
|
|
| 1911 |
i = p->f(p->left); // OK
|
| 1912 |
// ...
|
| 1913 |
}
|
| 1914 |
```
|
| 1915 |
|
| 1916 |
+
— *end example*]
|
| 1917 |
+
|
| 1918 |
+
If an *enum-head* contains a *nested-name-specifier*, the
|
| 1919 |
+
*enum-specifier* shall refer to an enumeration that was previously
|
| 1920 |
+
declared directly in the class or namespace to which the
|
| 1921 |
+
*nested-name-specifier* refers, or in an element of the inline namespace
|
| 1922 |
+
set ([[namespace.def]]) of that namespace (i.e., not merely inherited
|
| 1923 |
+
or introduced by a *using-declaration*), and the *enum-specifier* shall
|
| 1924 |
+
appear in a namespace enclosing the previous declaration. In such cases,
|
| 1925 |
+
the *nested-name-specifier* of the *enum-head* of the definition shall
|
| 1926 |
+
not begin with a *decltype-specifier*.
|
| 1927 |
+
|
| 1928 |
## Namespaces <a id="basic.namespace">[[basic.namespace]]</a>
|
| 1929 |
|
| 1930 |
A namespace is an optionally-named declarative region. The name of a
|
| 1931 |
namespace can be used to access entities declared in that namespace;
|
| 1932 |
that is, the members of the namespace. Unlike other declarative regions,
|
|
|
|
| 1936 |
The outermost declarative region of a translation unit is a namespace;
|
| 1937 |
see [[basic.scope.namespace]].
|
| 1938 |
|
| 1939 |
### Namespace definition <a id="namespace.def">[[namespace.def]]</a>
|
| 1940 |
|
|
|
|
|
|
|
| 1941 |
``` bnf
|
| 1942 |
namespace-name:
|
| 1943 |
+
identifier
|
| 1944 |
namespace-alias
|
| 1945 |
```
|
| 1946 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1947 |
``` bnf
|
| 1948 |
namespace-definition:
|
| 1949 |
named-namespace-definition
|
| 1950 |
unnamed-namespace-definition
|
| 1951 |
+
nested-namespace-definition
|
| 1952 |
```
|
| 1953 |
|
| 1954 |
``` bnf
|
| 1955 |
named-namespace-definition:
|
| 1956 |
+
'inline'ₒₚₜ 'namespace' attribute-specifier-seqₒₚₜ identifier '{' namespace-body '}'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1957 |
```
|
| 1958 |
|
| 1959 |
``` bnf
|
| 1960 |
unnamed-namespace-definition:
|
| 1961 |
+
'inline'ₒₚₜ 'namespace' attribute-specifier-seqₒₚₜ '{' namespace-body '}'
|
| 1962 |
+
```
|
| 1963 |
+
|
| 1964 |
+
``` bnf
|
| 1965 |
+
nested-namespace-definition:
|
| 1966 |
+
'namespace' enclosing-namespace-specifier '::' identifier '{' namespace-body '}'
|
| 1967 |
+
```
|
| 1968 |
+
|
| 1969 |
+
``` bnf
|
| 1970 |
+
enclosing-namespace-specifier:
|
| 1971 |
+
identifier
|
| 1972 |
+
enclosing-namespace-specifier '::' identifier
|
| 1973 |
```
|
| 1974 |
|
| 1975 |
``` bnf
|
| 1976 |
namespace-body:
|
| 1977 |
declaration-seqₒₚₜ
|
| 1978 |
```
|
| 1979 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1980 |
Every *namespace-definition* shall appear in the global scope or in a
|
| 1981 |
namespace scope ([[basic.scope.namespace]]).
|
| 1982 |
|
| 1983 |
+
In a *named-namespace-definition*, the *identifier* is the name of the
|
| 1984 |
+
namespace. If the *identifier*, when looked up (
|
| 1985 |
+
[[basic.lookup.unqual]]), refers to a *namespace-name* (but not a
|
| 1986 |
+
*namespace-alias*) that was introduced in the namespace in which the
|
| 1987 |
+
*named-namespace-definition* appears or that was introduced in a member
|
| 1988 |
+
of the inline namespace set of that namespace, the
|
| 1989 |
+
*namespace-definition* *extends* the previously-declared namespace.
|
| 1990 |
+
Otherwise, the *identifier* is introduced as a *namespace-name* into the
|
| 1991 |
+
declarative region in which the *named-namespace-definition* appears.
|
| 1992 |
+
|
| 1993 |
Because a *namespace-definition* contains *declaration*s in its
|
| 1994 |
*namespace-body* and a *namespace-definition* is itself a *declaration*,
|
| 1995 |
+
it follows that *namespace-definition*s can be nested.
|
| 1996 |
+
|
| 1997 |
+
[*Example 1*:
|
| 1998 |
|
| 1999 |
``` cpp
|
| 2000 |
namespace Outer {
|
| 2001 |
int i;
|
| 2002 |
namespace Inner {
|
|
|
|
| 2005 |
void g() { i++; } // Inner::i
|
| 2006 |
}
|
| 2007 |
}
|
| 2008 |
```
|
| 2009 |
|
| 2010 |
+
— *end example*]
|
| 2011 |
+
|
| 2012 |
The *enclosing namespaces* of a declaration are those namespaces in
|
| 2013 |
which the declaration lexically appears, except for a redeclaration of a
|
| 2014 |
namespace member outside its original namespace (e.g., a definition as
|
| 2015 |
specified in [[namespace.memdef]]). Such a redeclaration has the same
|
| 2016 |
enclosing namespaces as the original declaration.
|
| 2017 |
|
| 2018 |
+
[*Example 2*:
|
| 2019 |
+
|
| 2020 |
``` cpp
|
| 2021 |
namespace Q {
|
| 2022 |
namespace V {
|
| 2023 |
void f(); // enclosing namespaces are the global namespace, Q, and Q::V
|
| 2024 |
class C { void m(); };
|
|
|
|
| 2029 |
void V::C::m() { // enclosing namespaces are the global namespace, Q, and Q::V
|
| 2030 |
}
|
| 2031 |
}
|
| 2032 |
```
|
| 2033 |
|
| 2034 |
+
— *end example*]
|
| 2035 |
+
|
| 2036 |
If the optional initial `inline` keyword appears in a
|
| 2037 |
*namespace-definition* for a particular namespace, that namespace is
|
| 2038 |
declared to be an *inline namespace*. The `inline` keyword may be used
|
| 2039 |
+
on a *namespace-definition* that extends a namespace only if it was
|
| 2040 |
+
previously used on the *namespace-definition* that initially declared
|
| 2041 |
+
the *namespace-name* for that namespace.
|
| 2042 |
+
|
| 2043 |
+
The optional *attribute-specifier-seq* in a *named-namespace-definition*
|
| 2044 |
+
appertains to the namespace being defined or extended.
|
| 2045 |
|
| 2046 |
Members of an inline namespace can be used in most respects as though
|
| 2047 |
they were members of the enclosing namespace. Specifically, the inline
|
| 2048 |
namespace and its enclosing namespace are both added to the set of
|
| 2049 |
associated namespaces used in argument-dependent lookup (
|
| 2050 |
[[basic.lookup.argdep]]) whenever one of them is, and a
|
| 2051 |
*using-directive* ([[namespace.udir]]) that names the inline namespace
|
| 2052 |
is implicitly inserted into the enclosing namespace as for an unnamed
|
| 2053 |
namespace ([[namespace.unnamed]]). Furthermore, each member of the
|
| 2054 |
+
inline namespace can subsequently be partially specialized (
|
| 2055 |
+
[[temp.class.spec]]), explicitly instantiated ([[temp.explicit]]), or
|
| 2056 |
+
explicitly specialized ([[temp.expl.spec]]) as though it were a member
|
| 2057 |
+
of the enclosing namespace. Finally, looking up a name in the enclosing
|
| 2058 |
+
namespace via explicit qualification ([[namespace.qual]]) will include
|
| 2059 |
+
members of the inline namespace brought in by the *using-directive* even
|
| 2060 |
+
if there are declarations of that name in the enclosing namespace.
|
| 2061 |
|
| 2062 |
These properties are transitive: if a namespace `N` contains an inline
|
| 2063 |
namespace `M`, which in turn contains an inline namespace `O`, then the
|
| 2064 |
members of `O` can be used as though they were members of `M` or `N`.
|
| 2065 |
The *inline namespace set* of `N` is the transitive closure of all
|
| 2066 |
inline namespaces in `N`. The *enclosing namespace set* of `O` is the
|
| 2067 |
set of namespaces consisting of the innermost non-inline namespace
|
| 2068 |
enclosing an inline namespace `O`, together with any intervening inline
|
| 2069 |
namespaces.
|
| 2070 |
|
| 2071 |
+
A *nested-namespace-definition* with an *enclosing-namespace-specifier*
|
| 2072 |
+
`E`, *identifier* `I` and *namespace-body* `B` is equivalent to
|
| 2073 |
+
|
| 2074 |
+
``` cpp
|
| 2075 |
+
namespace E { namespace I { B } }
|
| 2076 |
+
```
|
| 2077 |
+
|
| 2078 |
+
[*Example 3*:
|
| 2079 |
+
|
| 2080 |
+
``` cpp
|
| 2081 |
+
namespace A::B::C {
|
| 2082 |
+
int i;
|
| 2083 |
+
}
|
| 2084 |
+
```
|
| 2085 |
+
|
| 2086 |
+
The above has the same effect as:
|
| 2087 |
+
|
| 2088 |
+
``` cpp
|
| 2089 |
+
namespace A {
|
| 2090 |
+
namespace B {
|
| 2091 |
+
namespace C {
|
| 2092 |
+
int i;
|
| 2093 |
+
}
|
| 2094 |
+
}
|
| 2095 |
+
}
|
| 2096 |
+
```
|
| 2097 |
+
|
| 2098 |
+
— *end example*]
|
| 2099 |
+
|
| 2100 |
#### Unnamed namespaces <a id="namespace.unnamed">[[namespace.unnamed]]</a>
|
| 2101 |
|
| 2102 |
An *unnamed-namespace-definition* behaves as if it were replaced by
|
| 2103 |
|
| 2104 |
``` bnf
|
| 2105 |
+
'inline'ₒₚₜ 'namespace' 'unique ' '{ /* empty body */ }'
|
| 2106 |
+
'using namespace' 'unique ' ';'
|
| 2107 |
+
'namespace' 'unique ' '{' namespace-body '}'
|
| 2108 |
```
|
| 2109 |
|
| 2110 |
where `inline` appears if and only if it appears in the
|
| 2111 |
+
*unnamed-namespace-definition* and all occurrences of `unique ` in a
|
| 2112 |
translation unit are replaced by the same identifier, and this
|
| 2113 |
+
identifier differs from all other identifiers in the translation unit.
|
| 2114 |
+
The optional *attribute-specifier-seq* in the
|
| 2115 |
+
*unnamed-namespace-definition* appertains to `unique `.
|
| 2116 |
+
|
| 2117 |
+
[*Example 1*:
|
| 2118 |
|
| 2119 |
``` cpp
|
| 2120 |
namespace { int i; } // unique ::i
|
| 2121 |
void f() { i++; } // unique ::i++
|
| 2122 |
|
|
|
|
| 2134 |
A::i++; // A::unique ::i
|
| 2135 |
j++; // A::unique ::j
|
| 2136 |
}
|
| 2137 |
```
|
| 2138 |
|
| 2139 |
+
— *end example*]
|
| 2140 |
+
|
| 2141 |
#### Namespace member definitions <a id="namespace.memdef">[[namespace.memdef]]</a>
|
| 2142 |
|
| 2143 |
+
A declaration in a namespace `N` (excluding declarations in nested
|
| 2144 |
+
scopes) whose *declarator-id* is an *unqualified-id* ([[dcl.meaning]]),
|
| 2145 |
+
whose *class-head-name* (Clause [[class]]) or *enum-head-name* (
|
| 2146 |
+
[[dcl.enum]]) is an *identifier*, or whose *elaborated-type-specifier*
|
| 2147 |
+
is of the form *class-key* *attribute-specifier-seq*ₒₚₜ *identifier* (
|
| 2148 |
+
[[dcl.type.elab]]), or that is an *opaque-enum-declaration*, declares
|
| 2149 |
+
(or redeclares) its *unqualified-id* or *identifier* as a member of `N`.
|
| 2150 |
+
|
| 2151 |
+
[*Note 1*: An explicit instantiation ([[temp.explicit]]) or explicit
|
| 2152 |
+
specialization ([[temp.expl.spec]]) of a template does not introduce a
|
| 2153 |
+
name and thus may be declared using an *unqualified-id* in a member of
|
| 2154 |
+
the enclosing namespace set, if the primary template is declared in an
|
| 2155 |
+
inline namespace. — *end note*]
|
| 2156 |
+
|
| 2157 |
+
[*Example 1*:
|
| 2158 |
|
| 2159 |
``` cpp
|
| 2160 |
namespace X {
|
| 2161 |
+
void f() { ... } // OK: introduces X::f()
|
| 2162 |
+
|
| 2163 |
+
namespace M {
|
| 2164 |
+
void g(); // OK: introduces X::M::g()
|
| 2165 |
+
}
|
| 2166 |
+
using M::g;
|
| 2167 |
+
void g(); // error: conflicts with X::M::g()
|
| 2168 |
}
|
| 2169 |
```
|
| 2170 |
|
| 2171 |
+
— *end example*]
|
| 2172 |
+
|
| 2173 |
Members of a named namespace can also be defined outside that namespace
|
| 2174 |
by explicit qualification ([[namespace.qual]]) of the name being
|
| 2175 |
defined, provided that the entity being defined was already declared in
|
| 2176 |
the namespace and the definition appears after the point of declaration
|
| 2177 |
in a namespace that encloses the declaration’s namespace.
|
| 2178 |
|
| 2179 |
+
[*Example 2*:
|
| 2180 |
+
|
| 2181 |
``` cpp
|
| 2182 |
namespace Q {
|
| 2183 |
namespace V {
|
| 2184 |
void f();
|
| 2185 |
}
|
| 2186 |
+
void V::f() { ... } // OK
|
| 2187 |
+
void V::g() { ... } // error: g() is not yet a member of V
|
| 2188 |
namespace V {
|
| 2189 |
void g();
|
| 2190 |
}
|
| 2191 |
}
|
| 2192 |
|
| 2193 |
namespace R {
|
| 2194 |
+
void Q::V::g() { ... } // error: R doesn't enclose Q
|
| 2195 |
}
|
| 2196 |
```
|
| 2197 |
|
| 2198 |
+
— *end example*]
|
| 2199 |
+
|
| 2200 |
If a `friend` declaration in a non-local class first declares a class,
|
| 2201 |
+
function, class template or function template[^5] the friend is a member
|
| 2202 |
of the innermost enclosing namespace. The `friend` declaration does not
|
| 2203 |
by itself make the name visible to unqualified lookup (
|
| 2204 |
[[basic.lookup.unqual]]) or qualified lookup ([[basic.lookup.qual]]).
|
| 2205 |
+
|
| 2206 |
+
[*Note 2*: The name of the friend will be visible in its namespace if a
|
| 2207 |
+
matching declaration is provided at namespace scope (either before or
|
| 2208 |
+
after the class definition granting friendship). — *end note*]
|
| 2209 |
+
|
| 2210 |
+
If a friend function or function template is called, its name may be
|
| 2211 |
+
found by the name lookup that considers functions from namespaces and
|
| 2212 |
+
classes associated with the types of the function arguments (
|
| 2213 |
+
[[basic.lookup.argdep]]). If the name in a `friend` declaration is
|
| 2214 |
+
neither qualified nor a *template-id* and the declaration is a function
|
| 2215 |
+
or an *elaborated-type-specifier*, the lookup to determine whether the
|
| 2216 |
+
entity has been previously declared shall not consider any scopes
|
| 2217 |
+
outside the innermost enclosing namespace.
|
| 2218 |
+
|
| 2219 |
+
[*Note 3*: The other forms of `friend` declarations cannot declare a
|
| 2220 |
+
new member of the innermost enclosing namespace and thus follow the
|
| 2221 |
+
usual lookup rules. — *end note*]
|
| 2222 |
+
|
| 2223 |
+
[*Example 3*:
|
| 2224 |
|
| 2225 |
``` cpp
|
| 2226 |
// Assume f and g have not yet been declared.
|
| 2227 |
void h(int);
|
| 2228 |
template <class T> void f2(T);
|
|
|
|
| 2238 |
};
|
| 2239 |
|
| 2240 |
// A::f, A::g and A::h are not visible here
|
| 2241 |
X x;
|
| 2242 |
void g() { f(x); } // definition of A::g
|
| 2243 |
+
void f(X) { ... } // definition of A::f
|
| 2244 |
+
void h(int) { ... } // definition of A::h
|
| 2245 |
// A::f, A::g and A::h are visible here and known to be friends
|
| 2246 |
}
|
| 2247 |
|
| 2248 |
using A::x;
|
| 2249 |
|
|
|
|
| 2252 |
A::X::f(x); // error: f is not a member of A::X
|
| 2253 |
A::X::Y::g(); // error: g is not a member of A::X::Y
|
| 2254 |
}
|
| 2255 |
```
|
| 2256 |
|
| 2257 |
+
— *end example*]
|
| 2258 |
+
|
| 2259 |
### Namespace alias <a id="namespace.alias">[[namespace.alias]]</a>
|
| 2260 |
|
| 2261 |
A *namespace-alias-definition* declares an alternate name for a
|
| 2262 |
namespace according to the following grammar:
|
| 2263 |
|
|
|
|
| 2276 |
nested-name-specifierₒₚₜ namespace-name
|
| 2277 |
```
|
| 2278 |
|
| 2279 |
The *identifier* in a *namespace-alias-definition* is a synonym for the
|
| 2280 |
name of the namespace denoted by the *qualified-namespace-specifier* and
|
| 2281 |
+
becomes a *namespace-alias*.
|
| 2282 |
+
|
| 2283 |
+
[*Note 1*: When looking up a *namespace-name* in a
|
| 2284 |
*namespace-alias-definition*, only namespace names are considered, see
|
| 2285 |
+
[[basic.lookup.udir]]. — *end note*]
|
| 2286 |
|
| 2287 |
In a declarative region, a *namespace-alias-definition* can be used to
|
| 2288 |
redefine a *namespace-alias* declared in that declarative region to
|
| 2289 |
+
refer only to the namespace to which it already refers.
|
| 2290 |
+
|
| 2291 |
+
[*Example 1*:
|
| 2292 |
+
|
| 2293 |
+
The following declarations are well-formed:
|
| 2294 |
|
| 2295 |
``` cpp
|
| 2296 |
+
namespace Company_with_very_long_name { ... }
|
| 2297 |
namespace CWVLN = Company_with_very_long_name;
|
| 2298 |
namespace CWVLN = Company_with_very_long_name; // OK: duplicate
|
| 2299 |
namespace CWVLN = CWVLN;
|
| 2300 |
```
|
| 2301 |
|
| 2302 |
+
— *end example*]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2303 |
|
| 2304 |
### The `using` declaration <a id="namespace.udecl">[[namespace.udecl]]</a>
|
| 2305 |
|
|
|
|
|
|
|
|
|
|
| 2306 |
``` bnf
|
| 2307 |
using-declaration:
|
| 2308 |
+
'using' using-declarator-list ';'
|
|
|
|
| 2309 |
```
|
| 2310 |
|
| 2311 |
+
``` bnf
|
| 2312 |
+
using-declarator-list:
|
| 2313 |
+
using-declarator '...'ₒₚₜ
|
| 2314 |
+
using-declarator-list ',' using-declarator '...'ₒₚₜ
|
| 2315 |
+
```
|
| 2316 |
+
|
| 2317 |
+
``` bnf
|
| 2318 |
+
using-declarator:
|
| 2319 |
+
'typename'ₒₚₜ nested-name-specifier unqualified-id
|
| 2320 |
+
```
|
| 2321 |
+
|
| 2322 |
+
Each *using-declarator* in a *using-declaration* [^6] introduces a set
|
| 2323 |
+
of declarations into the declarative region in which the
|
| 2324 |
+
*using-declaration* appears. The set of declarations introduced by the
|
| 2325 |
+
*using-declarator* is found by performing qualified name lookup (
|
| 2326 |
+
[[basic.lookup.qual]], [[class.member.lookup]]) for the name in the
|
| 2327 |
+
*using-declarator*, excluding functions that are hidden as described
|
| 2328 |
+
below. If the *using-declarator* does not name a constructor, the
|
| 2329 |
+
*unqualified-id* is declared in the declarative region in which the
|
| 2330 |
+
*using-declaration* appears as a synonym for each declaration introduced
|
| 2331 |
+
by the *using-declarator*.
|
| 2332 |
+
|
| 2333 |
+
[*Note 1*: Only the specified name is so declared; specifying an
|
| 2334 |
+
enumeration name in a *using-declaration* does not declare its
|
| 2335 |
+
enumerators in the *using-declaration*'s declarative
|
| 2336 |
+
region. — *end note*]
|
| 2337 |
+
|
| 2338 |
+
If the *using-declarator* names a constructor, it declares that the
|
| 2339 |
+
class *inherits* the set of constructor declarations introduced by the
|
| 2340 |
+
*using-declarator* from the nominated base class.
|
| 2341 |
|
| 2342 |
Every *using-declaration* is a *declaration* and a *member-declaration*
|
| 2343 |
+
and can therefore be used in a class definition.
|
| 2344 |
+
|
| 2345 |
+
[*Example 1*:
|
| 2346 |
|
| 2347 |
``` cpp
|
| 2348 |
struct B {
|
| 2349 |
void f(char);
|
| 2350 |
void g(char);
|
|
|
|
| 2357 |
void f(int) { f('c'); } // calls B::f(char)
|
| 2358 |
void g(int) { g('c'); } // recursively calls D::g(int)
|
| 2359 |
};
|
| 2360 |
```
|
| 2361 |
|
| 2362 |
+
— *end example*]
|
| 2363 |
+
|
| 2364 |
+
In a *using-declaration* used as a *member-declaration*, each
|
| 2365 |
+
*using-declarator*'s *nested-name-specifier* shall name a base class of
|
| 2366 |
+
the class being defined. If a *using-declarator* names a constructor,
|
| 2367 |
+
its *nested-name-specifier* shall name a direct base class of the class
|
| 2368 |
+
being defined.
|
| 2369 |
+
|
| 2370 |
+
[*Example 2*:
|
| 2371 |
+
|
| 2372 |
+
``` cpp
|
| 2373 |
+
template <typename... bases>
|
| 2374 |
+
struct X : bases... {
|
| 2375 |
+
using bases::g...;
|
| 2376 |
+
};
|
| 2377 |
+
|
| 2378 |
+
X<B, D> x; // OK: B::g and D::g introduced
|
| 2379 |
+
```
|
| 2380 |
+
|
| 2381 |
+
— *end example*]
|
| 2382 |
+
|
| 2383 |
+
[*Example 3*:
|
| 2384 |
|
| 2385 |
``` cpp
|
| 2386 |
class C {
|
| 2387 |
int g();
|
| 2388 |
};
|
|
|
|
| 2393 |
using B::x; // OK: x is a union member of base B
|
| 2394 |
using C::g; // error: C isn't a base of D2
|
| 2395 |
};
|
| 2396 |
```
|
| 2397 |
|
| 2398 |
+
— *end example*]
|
| 2399 |
+
|
| 2400 |
+
[*Note 2*: Since destructors do not have names, a *using-declaration*
|
| 2401 |
+
cannot refer to a destructor for a base class. Since specializations of
|
| 2402 |
+
member templates for conversion functions are not found by name lookup,
|
| 2403 |
+
they are not considered when a *using-declaration* specifies a
|
| 2404 |
+
conversion function ([[temp.mem]]). — *end note*]
|
| 2405 |
+
|
| 2406 |
+
If a constructor or assignment operator brought from a base class into a
|
| 2407 |
+
derived class has the signature of a copy/move constructor or assignment
|
| 2408 |
+
operator for the derived class ([[class.copy]]), the
|
| 2409 |
*using-declaration* does not by itself suppress the implicit declaration
|
| 2410 |
+
of the derived class member; the member from the base class is hidden or
|
| 2411 |
+
overridden by the implicitly-declared copy/move constructor or
|
| 2412 |
+
assignment operator of the derived class, as described below.
|
|
|
|
| 2413 |
|
| 2414 |
A *using-declaration* shall not name a *template-id*.
|
| 2415 |
|
| 2416 |
+
[*Example 4*:
|
| 2417 |
+
|
| 2418 |
``` cpp
|
| 2419 |
struct A {
|
| 2420 |
template <class T> void f(T);
|
| 2421 |
template <class T> struct X { };
|
| 2422 |
};
|
|
|
|
| 2424 |
using A::f<double>; // ill-formed
|
| 2425 |
using A::X<int>; // ill-formed
|
| 2426 |
};
|
| 2427 |
```
|
| 2428 |
|
| 2429 |
+
— *end example*]
|
| 2430 |
+
|
| 2431 |
A *using-declaration* shall not name a namespace.
|
| 2432 |
|
| 2433 |
A *using-declaration* shall not name a scoped enumerator.
|
| 2434 |
|
| 2435 |
+
A *using-declaration* that names a class member shall be a
|
| 2436 |
*member-declaration*.
|
| 2437 |
|
| 2438 |
+
[*Example 5*:
|
| 2439 |
+
|
| 2440 |
``` cpp
|
| 2441 |
struct X {
|
| 2442 |
int i;
|
| 2443 |
static int s;
|
| 2444 |
};
|
| 2445 |
|
| 2446 |
void f() {
|
| 2447 |
+
using X::i; // error: X::i is a class member and this is not a member declaration.
|
| 2448 |
+
using X::s; // error: X::s is a class member and this is not a member declaration.
|
|
|
|
|
|
|
| 2449 |
}
|
| 2450 |
```
|
| 2451 |
|
| 2452 |
+
— *end example*]
|
| 2453 |
+
|
| 2454 |
Members declared by a *using-declaration* can be referred to by explicit
|
| 2455 |
+
qualification just like other member names ([[namespace.qual]]).
|
| 2456 |
+
|
| 2457 |
+
[*Example 6*:
|
| 2458 |
|
| 2459 |
``` cpp
|
| 2460 |
void f();
|
| 2461 |
|
| 2462 |
namespace A {
|
|
|
|
| 2473 |
X::f(); // calls ::f
|
| 2474 |
X::g(); // calls A::g
|
| 2475 |
}
|
| 2476 |
```
|
| 2477 |
|
| 2478 |
+
— *end example*]
|
| 2479 |
+
|
| 2480 |
A *using-declaration* is a *declaration* and can therefore be used
|
| 2481 |
repeatedly where (and only where) multiple declarations are allowed.
|
| 2482 |
|
| 2483 |
+
[*Example 7*:
|
| 2484 |
+
|
| 2485 |
``` cpp
|
| 2486 |
namespace A {
|
| 2487 |
int i;
|
| 2488 |
}
|
| 2489 |
|
| 2490 |
namespace A1 {
|
| 2491 |
+
using A::i, A::i; // OK: double declaration
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2492 |
}
|
| 2493 |
|
| 2494 |
struct B {
|
| 2495 |
int i;
|
| 2496 |
};
|
| 2497 |
|
| 2498 |
struct X : B {
|
| 2499 |
+
using B::i, B::i; // error: double member declaration
|
|
|
|
| 2500 |
};
|
| 2501 |
```
|
| 2502 |
|
| 2503 |
+
— *end example*]
|
| 2504 |
+
|
| 2505 |
+
[*Note 3*: For a *using-declaration* whose *nested-name-specifier*
|
| 2506 |
+
names a namespace, members added to the namespace after the
|
| 2507 |
+
*using-declaration* are not in the set of introduced declarations, so
|
| 2508 |
+
they are not considered when a use of the name is made. Thus, additional
|
| 2509 |
+
overloads added after the *using-declaration* are ignored, but default
|
| 2510 |
+
function arguments ([[dcl.fct.default]]), default template arguments (
|
| 2511 |
[[temp.param]]), and template specializations ([[temp.class.spec]],
|
| 2512 |
+
[[temp.expl.spec]]) are considered. — *end note*]
|
| 2513 |
+
|
| 2514 |
+
[*Example 8*:
|
| 2515 |
|
| 2516 |
``` cpp
|
| 2517 |
namespace A {
|
| 2518 |
void f(int);
|
| 2519 |
}
|
| 2520 |
|
| 2521 |
+
using A::f; // f is a synonym for A::f; that is, for A::f(int).
|
|
|
|
| 2522 |
namespace A {
|
| 2523 |
void f(char);
|
| 2524 |
}
|
| 2525 |
|
| 2526 |
void foo() {
|
| 2527 |
+
f('a'); // calls f(int), even though f(char) exists.
|
| 2528 |
+
}
|
| 2529 |
|
| 2530 |
void bar() {
|
| 2531 |
+
using A::f; // f is a synonym for A::f; that is, for A::f(int) and A::f(char).
|
|
|
|
| 2532 |
f('a'); // calls f(char)
|
| 2533 |
}
|
| 2534 |
```
|
| 2535 |
|
| 2536 |
+
— *end example*]
|
| 2537 |
+
|
| 2538 |
+
[*Note 4*: Partial specializations of class templates are found by
|
| 2539 |
+
looking up the primary class template and then considering all partial
|
| 2540 |
+
specializations of that template. If a *using-declaration* names a class
|
| 2541 |
+
template, partial specializations introduced after the
|
| 2542 |
+
*using-declaration* are effectively visible because the primary template
|
| 2543 |
+
is visible ([[temp.class.spec]]). — *end note*]
|
| 2544 |
|
| 2545 |
Since a *using-declaration* is a declaration, the restrictions on
|
| 2546 |
declarations of the same name in the same declarative region (
|
| 2547 |
[[basic.scope]]) also apply to *using-declaration*s.
|
| 2548 |
|
| 2549 |
+
[*Example 9*:
|
| 2550 |
+
|
| 2551 |
``` cpp
|
| 2552 |
namespace A {
|
| 2553 |
int x;
|
| 2554 |
}
|
| 2555 |
|
|
|
|
| 2576 |
x = 99; // assigns to A::x
|
| 2577 |
struct x x1; // x1 has class type B::x
|
| 2578 |
}
|
| 2579 |
```
|
| 2580 |
|
| 2581 |
+
— *end example*]
|
| 2582 |
+
|
| 2583 |
If a function declaration in namespace scope or block scope has the same
|
| 2584 |
name and the same parameter-type-list ([[dcl.fct]]) as a function
|
| 2585 |
introduced by a *using-declaration*, and the declarations do not declare
|
| 2586 |
the same function, the program is ill-formed. If a function template
|
| 2587 |
declaration in namespace scope has the same name, parameter-type-list,
|
| 2588 |
return type, and template parameter list as a function template
|
| 2589 |
+
introduced by a *using-declaration*, the program is ill-formed.
|
| 2590 |
+
|
| 2591 |
+
[*Note 5*:
|
| 2592 |
+
|
| 2593 |
+
Two *using-declaration*s may introduce functions with the same name and
|
| 2594 |
+
the same parameter-type-list. If, for a call to an unqualified function
|
| 2595 |
name, function overload resolution selects the functions introduced by
|
| 2596 |
such *using-declaration*s, the function call is ill-formed.
|
| 2597 |
|
| 2598 |
+
[*Example 10*:
|
| 2599 |
+
|
| 2600 |
``` cpp
|
| 2601 |
namespace B {
|
| 2602 |
void f(int);
|
| 2603 |
void f(double);
|
| 2604 |
}
|
|
|
|
| 2615 |
f(1); // error: ambiguous: B::f(int) or C::f(int)?
|
| 2616 |
void f(int); // error: f(int) conflicts with C::f(int) and B::f(int)
|
| 2617 |
}
|
| 2618 |
```
|
| 2619 |
|
| 2620 |
+
— *end example*]
|
| 2621 |
+
|
| 2622 |
+
— *end note*]
|
| 2623 |
+
|
| 2624 |
+
When a *using-declarator* brings declarations from a base class into a
|
| 2625 |
+
derived class, member functions and member function templates in the
|
| 2626 |
derived class override and/or hide member functions and member function
|
| 2627 |
templates with the same name, parameter-type-list ([[dcl.fct]]),
|
| 2628 |
cv-qualification, and *ref-qualifier* (if any) in a base class (rather
|
| 2629 |
+
than conflicting). Such hidden or overridden declarations are excluded
|
| 2630 |
+
from the set of declarations introduced by the *using-declarator*.
|
| 2631 |
+
|
| 2632 |
+
[*Example 11*:
|
| 2633 |
|
| 2634 |
``` cpp
|
| 2635 |
struct B {
|
| 2636 |
virtual void f(int);
|
| 2637 |
virtual void f(char);
|
|
|
|
| 2655 |
p->f(1); // calls D::f(int)
|
| 2656 |
p->f('a'); // calls B::f(char)
|
| 2657 |
p->g(1); // calls B::g(int)
|
| 2658 |
p->g('a'); // calls D::g(char)
|
| 2659 |
}
|
| 2660 |
+
|
| 2661 |
+
struct B1 {
|
| 2662 |
+
B1(int);
|
| 2663 |
+
};
|
| 2664 |
+
|
| 2665 |
+
struct B2 {
|
| 2666 |
+
B2(int);
|
| 2667 |
+
};
|
| 2668 |
+
|
| 2669 |
+
struct D1 : B1, B2 {
|
| 2670 |
+
using B1::B1;
|
| 2671 |
+
using B2::B2;
|
| 2672 |
+
};
|
| 2673 |
+
D1 d1(0); // ill-formed: ambiguous
|
| 2674 |
+
|
| 2675 |
+
struct D2 : B1, B2 {
|
| 2676 |
+
using B1::B1;
|
| 2677 |
+
using B2::B2;
|
| 2678 |
+
D2(int); // OK: D2::D2(int) hides B1::B1(int) and B2::B2(int)
|
| 2679 |
+
};
|
| 2680 |
+
D2 d2(0); // calls D2::D2(int)
|
| 2681 |
```
|
| 2682 |
|
| 2683 |
+
— *end example*]
|
| 2684 |
+
|
| 2685 |
+
For the purpose of overload resolution, the functions that are
|
| 2686 |
+
introduced by a *using-declaration* into a derived class are treated as
|
| 2687 |
+
though they were members of the derived class. In particular, the
|
| 2688 |
implicit `this` parameter shall be treated as if it were a pointer to
|
| 2689 |
the derived class rather than to the base class. This has no effect on
|
| 2690 |
the type of the function, and in all other respects the function remains
|
| 2691 |
+
a member of the base class. Likewise, constructors that are introduced
|
| 2692 |
+
by a *using-declaration* are treated as though they were constructors of
|
| 2693 |
+
the derived class when looking up the constructors of the derived
|
| 2694 |
+
class ([[class.qual]]) or forming a set of overload candidates (
|
| 2695 |
+
[[over.match.ctor]], [[over.match.copy]], [[over.match.list]]). If such
|
| 2696 |
+
a constructor is selected to perform the initialization of an object of
|
| 2697 |
+
class type, all subobjects other than the base class from which the
|
| 2698 |
+
constructor originated are implicitly initialized (
|
| 2699 |
+
[[class.inhctor.init]]).
|
| 2700 |
+
|
| 2701 |
+
In a *using-declarator* that does not name a constructor, all members of
|
| 2702 |
+
the set of introduced declarations shall be accessible. In a
|
| 2703 |
+
*using-declarator* that names a constructor, no access check is
|
| 2704 |
+
performed. In particular, if a derived class uses a *using-declarator*
|
| 2705 |
+
to access a member of a base class, the member name shall be accessible.
|
| 2706 |
+
If the name is that of an overloaded member function, then all functions
|
| 2707 |
+
named shall be accessible. The base class members mentioned by a
|
| 2708 |
+
*using-declarator* shall be visible in the scope of at least one of the
|
| 2709 |
+
direct base classes of the class where the *using-declarator* is
|
| 2710 |
+
specified.
|
| 2711 |
+
|
| 2712 |
+
[*Note 6*:
|
| 2713 |
+
|
| 2714 |
+
Because a *using-declarator* designates a base class member (and not a
|
| 2715 |
+
member subobject or a member function of a base class subobject), a
|
| 2716 |
+
*using-declarator* cannot be used to resolve inherited member
|
| 2717 |
+
ambiguities.
|
| 2718 |
+
|
| 2719 |
+
[*Example 12*:
|
| 2720 |
|
| 2721 |
``` cpp
|
| 2722 |
struct A { int x(); };
|
| 2723 |
struct B : A { };
|
| 2724 |
struct C : A {
|
|
|
|
| 2729 |
struct D : B, C {
|
| 2730 |
using C::x;
|
| 2731 |
int x(double);
|
| 2732 |
};
|
| 2733 |
int f(D* d) {
|
| 2734 |
+
return d->x(); // error: overload resolution selects A::x, but A is an ambiguous base class
|
| 2735 |
}
|
| 2736 |
```
|
| 2737 |
|
| 2738 |
+
— *end example*]
|
| 2739 |
+
|
| 2740 |
+
— *end note*]
|
| 2741 |
+
|
| 2742 |
+
A synonym created by a *using-declaration* has the usual accessibility
|
| 2743 |
+
for a *member-declaration*. A *using-declarator* that names a
|
| 2744 |
+
constructor does not create a synonym; instead, the additional
|
| 2745 |
+
constructors are accessible if they would be accessible when used to
|
| 2746 |
+
construct an object of the corresponding base class, and the
|
| 2747 |
+
accessibility of the *using-declaration* is ignored.
|
| 2748 |
+
|
| 2749 |
+
[*Example 13*:
|
| 2750 |
|
| 2751 |
``` cpp
|
| 2752 |
class A {
|
| 2753 |
private:
|
| 2754 |
void f(char);
|
|
|
|
| 2763 |
public:
|
| 2764 |
using A::g; // B::g is a public synonym for A::g
|
| 2765 |
};
|
| 2766 |
```
|
| 2767 |
|
| 2768 |
+
— *end example*]
|
| 2769 |
+
|
| 2770 |
+
If a *using-declarator* uses the keyword `typename` and specifies a
|
| 2771 |
dependent name ([[temp.dep]]), the name introduced by the
|
| 2772 |
*using-declaration* is treated as a *typedef-name* ([[dcl.typedef]]).
|
| 2773 |
|
| 2774 |
### Using directive <a id="namespace.udir">[[namespace.udir]]</a>
|
| 2775 |
|
|
|
|
| 2777 |
using-directive:
|
| 2778 |
attribute-specifier-seqₒₚₜ 'using namespace' nested-name-specifierₒₚₜ namespace-name ';'
|
| 2779 |
```
|
| 2780 |
|
| 2781 |
A *using-directive* shall not appear in class scope, but may appear in
|
| 2782 |
+
namespace scope or in block scope.
|
| 2783 |
+
|
| 2784 |
+
[*Note 1*: When looking up a *namespace-name* in a *using-directive*,
|
| 2785 |
+
only namespace names are considered, see
|
| 2786 |
+
[[basic.lookup.udir]]. — *end note*]
|
| 2787 |
+
|
| 2788 |
+
The optional *attribute-specifier-seq* appertains to the
|
| 2789 |
+
*using-directive*.
|
| 2790 |
|
| 2791 |
A *using-directive* specifies that the names in the nominated namespace
|
| 2792 |
can be used in the scope in which the *using-directive* appears after
|
| 2793 |
the *using-directive*. During unqualified name lookup (
|
| 2794 |
[[basic.lookup.unqual]]), the names appear as if they were declared in
|
| 2795 |
the nearest enclosing namespace which contains both the
|
| 2796 |
+
*using-directive* and the nominated namespace.
|
| 2797 |
+
|
| 2798 |
+
[*Note 2*: In this context, “contains” means “contains directly or
|
| 2799 |
+
indirectly”. — *end note*]
|
| 2800 |
|
| 2801 |
A *using-directive* does not add any members to the declarative region
|
| 2802 |
in which it appears.
|
| 2803 |
|
| 2804 |
+
[*Example 1*:
|
| 2805 |
+
|
| 2806 |
``` cpp
|
| 2807 |
namespace A {
|
| 2808 |
int i;
|
| 2809 |
namespace B {
|
| 2810 |
namespace C {
|
|
|
|
| 2829 |
void f4() {
|
| 2830 |
i = 5; // ill-formed; neither i is visible
|
| 2831 |
}
|
| 2832 |
```
|
| 2833 |
|
| 2834 |
+
— *end example*]
|
| 2835 |
+
|
| 2836 |
For unqualified lookup ([[basic.lookup.unqual]]), the *using-directive*
|
| 2837 |
is transitive: if a scope contains a *using-directive* that nominates a
|
| 2838 |
second namespace that itself contains *using-directive*s, the effect is
|
| 2839 |
as if the *using-directive*s from the second namespace also appeared in
|
| 2840 |
+
the first.
|
| 2841 |
+
|
| 2842 |
+
[*Note 3*: For qualified lookup, see
|
| 2843 |
+
[[namespace.qual]]. — *end note*]
|
| 2844 |
+
|
| 2845 |
+
[*Example 2*:
|
| 2846 |
|
| 2847 |
``` cpp
|
| 2848 |
namespace M {
|
| 2849 |
int i;
|
| 2850 |
}
|
|
|
|
| 2883 |
int n = j; // D::j hides B::j
|
| 2884 |
}
|
| 2885 |
}
|
| 2886 |
```
|
| 2887 |
|
| 2888 |
+
— *end example*]
|
| 2889 |
+
|
| 2890 |
+
If a namespace is extended ([[namespace.def]]) after a
|
| 2891 |
+
*using-directive* for that namespace is given, the additional members of
|
| 2892 |
+
the extended namespace and the members of namespaces nominated by
|
| 2893 |
+
*using-directive*s in the extending *namespace-definition* can be used
|
| 2894 |
+
after the extending *namespace-definition*.
|
| 2895 |
|
| 2896 |
If name lookup finds a declaration for a name in two different
|
| 2897 |
namespaces, and the declarations do not declare the same entity and do
|
| 2898 |
+
not declare functions, the use of the name is ill-formed.
|
| 2899 |
+
|
| 2900 |
+
[*Note 4*:
|
| 2901 |
+
|
| 2902 |
+
In particular, the name of a variable, function or enumerator does not
|
| 2903 |
+
hide the name of a class or enumeration declared in a different
|
| 2904 |
+
namespace. For example,
|
| 2905 |
|
| 2906 |
``` cpp
|
| 2907 |
namespace A {
|
| 2908 |
class X { };
|
| 2909 |
extern "C" int g();
|
|
|
|
| 2917 |
using namespace A;
|
| 2918 |
using namespace B;
|
| 2919 |
|
| 2920 |
void f() {
|
| 2921 |
X(1); // error: name X found in two namespaces
|
| 2922 |
+
g(); // OK: name g refers to the same entity
|
| 2923 |
+
h(); // OK: overload resolution selects A::h
|
| 2924 |
}
|
| 2925 |
```
|
| 2926 |
|
| 2927 |
+
— *end note*]
|
| 2928 |
+
|
| 2929 |
During overload resolution, all functions from the transitive search are
|
| 2930 |
considered for argument matching. The set of declarations found by the
|
| 2931 |
+
transitive search is unordered.
|
| 2932 |
+
|
| 2933 |
+
[*Note 5*: In particular, the order in which namespaces were considered
|
| 2934 |
+
and the relationships among the namespaces implied by the
|
| 2935 |
+
*using-directive*s do not cause preference to be given to any of the
|
| 2936 |
+
declarations found by the search. — *end note*]
|
| 2937 |
+
|
| 2938 |
+
An ambiguity exists if the best match finds two functions with the same
|
| 2939 |
+
signature, even if one is in a namespace reachable through
|
| 2940 |
+
*using-directive*s in the namespace of the other.[^7]
|
| 2941 |
+
|
| 2942 |
+
[*Example 3*:
|
| 2943 |
|
| 2944 |
``` cpp
|
| 2945 |
namespace D {
|
| 2946 |
int d1;
|
| 2947 |
void f(char);
|
|
|
|
| 2970 |
f(1); // error: ambiguous: D::f(int) or E::f(int)?
|
| 2971 |
f('a'); // OK: D::f(char)
|
| 2972 |
}
|
| 2973 |
```
|
| 2974 |
|
| 2975 |
+
— *end example*]
|
| 2976 |
+
|
| 2977 |
## The `asm` declaration <a id="dcl.asm">[[dcl.asm]]</a>
|
| 2978 |
|
| 2979 |
An `asm` declaration has the form
|
| 2980 |
|
| 2981 |
``` bnf
|
| 2982 |
asm-definition:
|
| 2983 |
+
attribute-specifier-seqₒₚₜ 'asm (' string-literal ') ;'
|
| 2984 |
```
|
| 2985 |
|
| 2986 |
The `asm` declaration is conditionally-supported; its meaning is
|
| 2987 |
+
*implementation-defined*. The optional *attribute-specifier-seq* in an
|
| 2988 |
+
*asm-definition* appertains to the `asm` declaration.
|
| 2989 |
+
|
| 2990 |
+
[*Note 1*: Typically it is used to pass information through the
|
| 2991 |
+
implementation to an assembler. — *end note*]
|
| 2992 |
|
| 2993 |
## Linkage specifications <a id="dcl.link">[[dcl.link]]</a>
|
| 2994 |
|
| 2995 |
All function types, function names with external linkage, and variable
|
| 2996 |
+
names with external linkage have a *language linkage*.
|
| 2997 |
+
|
| 2998 |
+
[*Note 1*: Some of the properties associated with an entity with
|
| 2999 |
+
language linkage are specific to each implementation and are not
|
| 3000 |
+
described here. For example, a particular language linkage may be
|
| 3001 |
+
associated with a particular form of representing names of objects and
|
| 3002 |
+
functions with external linkage, or with a particular calling
|
| 3003 |
+
convention, etc. — *end note*]
|
| 3004 |
+
|
| 3005 |
+
The default language linkage of all function types, function names, and
|
| 3006 |
+
variable names is C++language linkage. Two function types with different
|
| 3007 |
+
language linkages are distinct types even if they are otherwise
|
| 3008 |
+
identical.
|
| 3009 |
|
| 3010 |
Linkage ([[basic.link]]) between C++and non-C++code fragments can be
|
| 3011 |
achieved using a *linkage-specification*:
|
| 3012 |
|
| 3013 |
``` bnf
|
|
|
|
| 3018 |
|
| 3019 |
The *string-literal* indicates the required language linkage. This
|
| 3020 |
International Standard specifies the semantics for the *string-literal*s
|
| 3021 |
`"C"` and `"C++"`. Use of a *string-literal* other than `"C"` or `"C++"`
|
| 3022 |
is conditionally-supported, with *implementation-defined* semantics.
|
| 3023 |
+
|
| 3024 |
+
[*Note 2*: Therefore, a linkage-specification with a *string-literal*
|
| 3025 |
+
that is unknown to the implementation requires a
|
| 3026 |
+
diagnostic. — *end note*]
|
| 3027 |
+
|
| 3028 |
+
[*Note 3*: It is recommended that the spelling of the *string-literal*
|
| 3029 |
+
be taken from the document defining that language. For example, `Ada`
|
| 3030 |
+
(not `ADA`) and `Fortran` or `FORTRAN`, depending on the
|
| 3031 |
+
vintage. — *end note*]
|
| 3032 |
|
| 3033 |
Every implementation shall provide for linkage to functions written in
|
| 3034 |
the C programming language, `"C"`, and linkage to C++functions, `"C++"`.
|
| 3035 |
|
| 3036 |
+
[*Example 1*:
|
| 3037 |
+
|
| 3038 |
``` cpp
|
| 3039 |
complex sqrt(complex); // C++linkage by default
|
| 3040 |
extern "C" {
|
| 3041 |
double sqrt(double); // C linkage
|
| 3042 |
}
|
| 3043 |
```
|
| 3044 |
|
| 3045 |
+
— *end example*]
|
| 3046 |
+
|
| 3047 |
Linkage specifications nest. When linkage specifications nest, the
|
| 3048 |
innermost one determines the language linkage. A linkage specification
|
| 3049 |
does not establish a scope. A *linkage-specification* shall occur only
|
| 3050 |
in namespace scope ([[basic.scope]]). In a *linkage-specification*, the
|
| 3051 |
specified language linkage applies to the function types of all function
|
| 3052 |
declarators, function names with external linkage, and variable names
|
| 3053 |
with external linkage declared within the *linkage-specification*.
|
| 3054 |
|
| 3055 |
+
[*Example 2*:
|
| 3056 |
+
|
| 3057 |
``` cpp
|
| 3058 |
+
extern "C" // the name f1 and its function type have C language linkage;
|
| 3059 |
+
void f1(void(*pf)(int)); // pf is a pointer to a C function
|
| 3060 |
+
|
| 3061 |
extern "C" typedef void FUNC();
|
| 3062 |
FUNC f2; // the name f2 has C++language linkage and the
|
| 3063 |
// function's type has C language linkage
|
| 3064 |
+
|
| 3065 |
+
extern "C" FUNC f3; // the name of function f3 and the function's type have C language linkage
|
| 3066 |
+
|
| 3067 |
+
void (*pf2)(FUNC*); // the name of the variable pf2 has C++linkage and the type
|
| 3068 |
+
// of pf2 is ``pointer to C++function that takes one parameter of type
|
| 3069 |
+
// pointer to C function''
|
| 3070 |
extern "C" {
|
| 3071 |
+
static void f4(); // the name of the function f4 has internal linkage (not C language linkage)
|
| 3072 |
+
// and the function's type has C language linkage.
|
|
|
|
|
|
|
| 3073 |
}
|
| 3074 |
|
| 3075 |
extern "C" void f5() {
|
| 3076 |
+
extern void f4(); // OK: Name linkage (internal) and function type linkage (C language linkage)
|
| 3077 |
+
// obtained from previous declaration.
|
|
|
|
|
|
|
| 3078 |
}
|
| 3079 |
|
| 3080 |
+
extern void f4(); // OK: Name linkage (internal) and function type linkage (C language linkage)
|
| 3081 |
+
// obtained from previous declaration.
|
|
|
|
|
|
|
| 3082 |
|
| 3083 |
void f6() {
|
| 3084 |
+
extern void f4(); // OK: Name linkage (internal) and function type linkage (C language linkage)
|
| 3085 |
+
// obtained from previous declaration.
|
|
|
|
|
|
|
| 3086 |
}
|
| 3087 |
```
|
| 3088 |
|
| 3089 |
+
— *end example*]
|
| 3090 |
+
|
| 3091 |
A C language linkage is ignored in determining the language linkage of
|
| 3092 |
the names of class members and the function type of class member
|
| 3093 |
functions.
|
| 3094 |
|
| 3095 |
+
[*Example 3*:
|
| 3096 |
+
|
| 3097 |
``` cpp
|
| 3098 |
extern "C" typedef void FUNC_c();
|
| 3099 |
+
|
| 3100 |
class C {
|
| 3101 |
+
void mf1(FUNC_c*); // the name of the function mf1 and the member function's type have
|
| 3102 |
+
// C++language linkage; the parameter has type ``pointer to C function''
|
| 3103 |
+
|
| 3104 |
+
FUNC_c mf2; // the name of the function mf2 and the member function's type have
|
| 3105 |
+
// C++language linkage
|
| 3106 |
+
|
| 3107 |
+
static FUNC_c* q; // the name of the data member q has C++language linkage and
|
| 3108 |
+
// the data member's type is ``pointer to C function''
|
| 3109 |
};
|
| 3110 |
|
| 3111 |
extern "C" {
|
| 3112 |
class X {
|
| 3113 |
+
void mf(); // the name of the function mf and the member function's type have
|
| 3114 |
+
// C++language linkage
|
| 3115 |
+
void mf2(void(*)()); // the name of the function mf2 has C++language linkage;
|
| 3116 |
+
// the parameter has type ``pointer to C function''
|
|
|
|
| 3117 |
};
|
| 3118 |
}
|
| 3119 |
```
|
| 3120 |
|
| 3121 |
+
— *end example*]
|
| 3122 |
+
|
| 3123 |
If two declarations declare functions with the same name and
|
| 3124 |
+
parameter-type-list ([[dcl.fct]]) to be members of the same namespace
|
| 3125 |
or declare objects with the same name to be members of the same
|
| 3126 |
namespace and the declarations give the names different language
|
| 3127 |
linkages, the program is ill-formed; no diagnostic is required if the
|
| 3128 |
declarations appear in different translation units. Except for functions
|
| 3129 |
with C++linkage, a function declaration without a linkage specification
|
|
|
|
| 3138 |
function name (ignoring the namespace names that qualify it) that appear
|
| 3139 |
in different namespace scopes refer to the same function. Two
|
| 3140 |
declarations for a variable with C language linkage with the same name
|
| 3141 |
(ignoring the namespace names that qualify it) that appear in different
|
| 3142 |
namespace scopes refer to the same variable. An entity with C language
|
| 3143 |
+
linkage shall not be declared with the same name as a variable in global
|
| 3144 |
scope, unless both declarations denote the same entity; no diagnostic is
|
| 3145 |
required if the declarations appear in different translation units. A
|
| 3146 |
variable with C language linkage shall not be declared with the same
|
| 3147 |
name as a function with C language linkage (ignoring the namespace names
|
| 3148 |
that qualify the respective names); no diagnostic is required if the
|
| 3149 |
+
declarations appear in different translation units.
|
| 3150 |
+
|
| 3151 |
+
[*Note 4*: Only one definition for an entity with a given name with C
|
| 3152 |
+
language linkage may appear in the program (see [[basic.def.odr]]);
|
| 3153 |
+
this implies that such an entity must not be defined in more than one
|
| 3154 |
+
namespace scope. — *end note*]
|
| 3155 |
+
|
| 3156 |
+
[*Example 4*:
|
| 3157 |
|
| 3158 |
``` cpp
|
| 3159 |
int x;
|
| 3160 |
namespace A {
|
| 3161 |
extern "C" int f();
|
|
|
|
| 3164 |
extern "C" int x(); // ill-formed: same name as global-space object x
|
| 3165 |
}
|
| 3166 |
|
| 3167 |
namespace B {
|
| 3168 |
extern "C" int f(); // A::f and B::f refer to the same function
|
| 3169 |
+
extern "C" int g() { return 1; } // ill-formed, the function g with C language linkage has two definitions
|
|
|
|
| 3170 |
}
|
| 3171 |
|
| 3172 |
int A::f() { return 98; } // definition for the function f with C language linkage
|
| 3173 |
extern "C" int h() { return 97; } // definition for the function h with C language linkage
|
| 3174 |
// A::h and ::h refer to the same function
|
| 3175 |
```
|
| 3176 |
|
| 3177 |
+
— *end example*]
|
| 3178 |
+
|
| 3179 |
A declaration directly contained in a *linkage-specification* is treated
|
| 3180 |
as if it contains the `extern` specifier ([[dcl.stc]]) for the purpose
|
| 3181 |
of determining the linkage of the declared name and whether it is a
|
| 3182 |
definition. Such a declaration shall not specify a storage class.
|
| 3183 |
|
| 3184 |
+
[*Example 5*:
|
| 3185 |
+
|
| 3186 |
``` cpp
|
| 3187 |
extern "C" double f();
|
| 3188 |
static double f(); // error
|
| 3189 |
extern "C" int i; // declaration
|
| 3190 |
extern "C" {
|
| 3191 |
int i; // definition
|
| 3192 |
}
|
| 3193 |
extern "C" static void g(); // error
|
| 3194 |
```
|
| 3195 |
|
| 3196 |
+
— *end example*]
|
| 3197 |
+
|
| 3198 |
+
[*Note 5*: Because the language linkage is part of a function type,
|
| 3199 |
+
when indirecting through a pointer to C function, the function to which
|
| 3200 |
+
the resulting lvalue refers is considered a C function. — *end note*]
|
| 3201 |
|
| 3202 |
Linkage from C++to objects defined in other languages and to objects
|
| 3203 |
+
defined in C++from other languages is *implementation-defined* and
|
| 3204 |
language-dependent. Only where the object layout strategies of two
|
| 3205 |
language implementations are similar enough can such linkage be
|
| 3206 |
achieved.
|
| 3207 |
|
| 3208 |
## Attributes <a id="dcl.attr">[[dcl.attr]]</a>
|
|
|
|
| 3217 |
attribute-specifier-seqₒₚₜ attribute-specifier
|
| 3218 |
```
|
| 3219 |
|
| 3220 |
``` bnf
|
| 3221 |
attribute-specifier:
|
| 3222 |
+
'[' '[' attribute-using-prefixₒₚₜ attribute-list ']' ']'
|
| 3223 |
alignment-specifier
|
| 3224 |
```
|
| 3225 |
|
| 3226 |
``` bnf
|
| 3227 |
alignment-specifier:
|
| 3228 |
'alignas (' type-id '...'ₒₚₜ ')'
|
| 3229 |
'alignas (' constant-expression '...'ₒₚₜ ')'
|
| 3230 |
```
|
| 3231 |
|
| 3232 |
+
``` bnf
|
| 3233 |
+
attribute-using-prefix:
|
| 3234 |
+
'using' attribute-namespace ':'
|
| 3235 |
+
```
|
| 3236 |
+
|
| 3237 |
``` bnf
|
| 3238 |
attribute-list:
|
| 3239 |
attributeₒₚₜ
|
| 3240 |
attribute-list ',' attributeₒₚₜ
|
| 3241 |
attribute '...'
|
|
|
|
| 3263 |
identifier
|
| 3264 |
```
|
| 3265 |
|
| 3266 |
``` bnf
|
| 3267 |
attribute-argument-clause:
|
| 3268 |
+
'(' balanced-token-seqₒₚₜ ')'
|
| 3269 |
```
|
| 3270 |
|
| 3271 |
``` bnf
|
| 3272 |
balanced-token-seq:
|
| 3273 |
+
balanced-token
|
| 3274 |
balanced-token-seq balanced-token
|
| 3275 |
```
|
| 3276 |
|
| 3277 |
``` bnf
|
| 3278 |
balanced-token:
|
| 3279 |
+
'(' balanced-token-seqₒₚₜ ')'
|
| 3280 |
+
'[' balanced-token-seqₒₚₜ ']'
|
| 3281 |
+
'{' balanced-token-seqₒₚₜ '}'
|
| 3282 |
any *token* other than a parenthesis, a bracket, or a brace
|
| 3283 |
```
|
| 3284 |
|
| 3285 |
+
If an *attribute-specifier* contains an *attribute-using-prefix*, the
|
| 3286 |
+
*attribute-list* following that *attribute-using-prefix* shall not
|
| 3287 |
+
contain an *attribute-scoped-token* and every *attribute-token* in that
|
| 3288 |
+
*attribute-list* is treated as if its *identifier* were prefixed with
|
| 3289 |
+
`N::`, where `N` is the *attribute-namespace* specified in the
|
| 3290 |
+
*attribute-using-prefix*.
|
| 3291 |
+
|
| 3292 |
+
[*Note 1*: This rule imposes no constraints on how an
|
| 3293 |
+
*attribute-using-prefix* affects the tokens in an
|
| 3294 |
+
*attribute-argument-clause*. — *end note*]
|
| 3295 |
+
|
| 3296 |
+
[*Example 1*:
|
| 3297 |
+
|
| 3298 |
+
``` cpp
|
| 3299 |
+
[[using CC: opt(1), debug]] // same as [[CC::opt(1), CC::debug]]
|
| 3300 |
+
void f() {}
|
| 3301 |
+
[[using CC: opt(1)]] [[CC::debug]] // same as [[CC::opt(1)]] [[CC::debug]]
|
| 3302 |
+
void g() {}
|
| 3303 |
+
[[using CC: CC::opt(1)]] // error: cannot combine using and scoped attribute token
|
| 3304 |
+
void h() {}
|
| 3305 |
+
```
|
| 3306 |
+
|
| 3307 |
+
— *end example*]
|
| 3308 |
+
|
| 3309 |
+
[*Note 2*: For each individual attribute, the form of the
|
| 3310 |
+
*balanced-token-seq* will be specified. — *end note*]
|
| 3311 |
|
| 3312 |
In an *attribute-list*, an ellipsis may appear only if that
|
| 3313 |
*attribute*’s specification permits it. An *attribute* followed by an
|
| 3314 |
ellipsis is a pack expansion ([[temp.variadic]]). An
|
| 3315 |
*attribute-specifier* that contains no *attribute*s has no effect. The
|
| 3316 |
+
order in which the *attribute-token*s appear in an *attribute-list* is
|
| 3317 |
not significant. If a keyword ([[lex.key]]) or an alternative token (
|
| 3318 |
[[lex.digraph]]) that satisfies the syntactic requirements of an
|
| 3319 |
*identifier* ([[lex.name]]) is contained in an *attribute-token*, it is
|
| 3320 |
considered an identifier. No name lookup ([[basic.lookup]]) is
|
| 3321 |
performed on any of the identifiers contained in an *attribute-token*.
|
| 3322 |
The *attribute-token* determines additional requirements on the
|
| 3323 |
+
*attribute-argument-clause* (if any).
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3324 |
|
| 3325 |
Each *attribute-specifier-seq* is said to *appertain* to some entity or
|
| 3326 |
statement, identified by the syntactic context where it appears (Clause
|
| 3327 |
[[stmt.stmt]], Clause [[dcl.dcl]], Clause [[dcl.decl]]). If an
|
| 3328 |
*attribute-specifier-seq* that appertains to some entity or statement
|
| 3329 |
+
contains an *attribute* or *alignment-specifier* that is not allowed to
|
| 3330 |
+
apply to that entity or statement, the program is ill-formed. If an
|
| 3331 |
+
*attribute-specifier-seq* appertains to a friend declaration (
|
| 3332 |
+
[[class.friend]]), that declaration shall be a definition. No
|
| 3333 |
+
*attribute-specifier-seq* shall appertain to an explicit instantiation (
|
| 3334 |
+
[[temp.explicit]]).
|
| 3335 |
|
| 3336 |
+
For an *attribute-token* (including an *attribute-scoped-token*) not
|
| 3337 |
+
specified in this International Standard, the behavior is
|
| 3338 |
+
*implementation-defined*. Any *attribute-token* that is not recognized
|
| 3339 |
+
by the implementation is ignored.
|
| 3340 |
+
|
| 3341 |
+
[*Note 3*: Each implementation should choose a distinctive name for the
|
| 3342 |
+
*attribute-namespace* in an *attribute-scoped-token*. — *end note*]
|
| 3343 |
|
| 3344 |
Two consecutive left square bracket tokens shall appear only when
|
| 3345 |
+
introducing an *attribute-specifier* or within the *balanced-token-seq*
|
| 3346 |
+
of an *attribute-argument-clause*.
|
| 3347 |
+
|
| 3348 |
+
[*Note 4*: If two consecutive left square brackets appear where an
|
| 3349 |
+
*attribute-specifier* is not allowed, the program is ill-formed even if
|
| 3350 |
+
the brackets match an alternative grammar production. — *end note*]
|
| 3351 |
+
|
| 3352 |
+
[*Example 2*:
|
| 3353 |
|
| 3354 |
``` cpp
|
| 3355 |
int p[10];
|
| 3356 |
void f() {
|
| 3357 |
int x = 42, y[5];
|
| 3358 |
+
int(p[[x] { return x; }()]); // error: invalid attribute on a nested declarator-id and
|
| 3359 |
+
// not a function-style cast of an element of p.
|
| 3360 |
+
y[[] { return 2; }()] = 2; // error even though attributes are not allowed in this context.
|
| 3361 |
+
int i [[vendor::attr([[]])]]; // well-formed implementation-defined attribute.
|
|
|
|
| 3362 |
}
|
| 3363 |
```
|
| 3364 |
|
| 3365 |
+
— *end example*]
|
| 3366 |
+
|
| 3367 |
### Alignment specifier <a id="dcl.align">[[dcl.align]]</a>
|
| 3368 |
|
| 3369 |
An *alignment-specifier* may be applied to a variable or to a class data
|
| 3370 |
member, but it shall not be applied to a bit-field, a function
|
| 3371 |
+
parameter, or an *exception-declaration* ([[except.handle]]). An
|
|
|
|
| 3372 |
*alignment-specifier* may also be applied to the declaration or
|
| 3373 |
definition of a class (in an *elaborated-type-specifier* (
|
| 3374 |
[[dcl.type.elab]]) or *class-head* (Clause [[class]]), respectively)
|
| 3375 |
and to the declaration or definition of an enumeration (in an
|
| 3376 |
*opaque-enum-declaration* or *enum-head*, respectively ([[dcl.enum]])).
|
|
|
|
| 3379 |
|
| 3380 |
When the *alignment-specifier* is of the form `alignas(`
|
| 3381 |
*constant-expression* `)`:
|
| 3382 |
|
| 3383 |
- the *constant-expression* shall be an integral constant expression
|
| 3384 |
+
- if the constant expression does not evaluate to an alignment value (
|
| 3385 |
+
[[basic.align]]), or evaluates to an extended alignment and the
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3386 |
implementation does not support that alignment in the context of the
|
| 3387 |
+
declaration, the program is ill-formed.
|
|
|
|
|
|
|
|
|
|
| 3388 |
|
| 3389 |
+
An *alignment-specifier* of the form `alignas(` *type-id* `)` has the
|
| 3390 |
+
same effect as `alignas({}alignof(` *type-id* `))` ([[expr.alignof]]).
|
|
|
|
| 3391 |
|
| 3392 |
+
The alignment requirement of an entity is the strictest nonzero
|
| 3393 |
+
alignment specified by its *alignment-specifier*s, if any; otherwise,
|
| 3394 |
+
the *alignment-specifier*s have no effect.
|
| 3395 |
|
| 3396 |
The combined effect of all *alignment-specifier*s in a declaration shall
|
| 3397 |
not specify an alignment that is less strict than the alignment that
|
| 3398 |
would be required for the entity being declared if all
|
| 3399 |
+
*alignment-specifier*s appertaining to that entity were omitted.
|
| 3400 |
+
|
| 3401 |
+
[*Example 1*:
|
| 3402 |
+
|
| 3403 |
+
``` cpp
|
| 3404 |
+
struct alignas(8) S {};
|
| 3405 |
+
struct alignas(1) U {
|
| 3406 |
+
S s;
|
| 3407 |
+
}; // error: U specifies an alignment that is less strict than if the alignas(1) were omitted.
|
| 3408 |
+
```
|
| 3409 |
+
|
| 3410 |
+
— *end example*]
|
| 3411 |
|
| 3412 |
If the defining declaration of an entity has an *alignment-specifier*,
|
| 3413 |
any non-defining declaration of that entity shall either specify
|
| 3414 |
equivalent alignment or have no *alignment-specifier*. Conversely, if
|
| 3415 |
any declaration of an entity has an *alignment-specifier*, every
|
| 3416 |
defining declaration of that entity shall specify an equivalent
|
| 3417 |
alignment. No diagnostic is required if declarations of an entity have
|
| 3418 |
different *alignment-specifier*s in different translation units.
|
| 3419 |
|
| 3420 |
+
[*Example 2*:
|
| 3421 |
+
|
| 3422 |
``` cpp
|
| 3423 |
// Translation unit #1:
|
| 3424 |
+
struct S { int x; } s, *p = &s;
|
| 3425 |
|
| 3426 |
// Translation unit #2:
|
| 3427 |
+
struct alignas(16) S; // error: definition of S lacks alignment, no diagnostic required
|
| 3428 |
+
extern S* p;
|
| 3429 |
```
|
| 3430 |
|
| 3431 |
+
— *end example*]
|
| 3432 |
+
|
| 3433 |
+
[*Example 3*:
|
| 3434 |
+
|
| 3435 |
An aligned buffer with an alignment requirement of `A` and holding `N`
|
| 3436 |
+
elements of type `T` can be declared as:
|
|
|
|
| 3437 |
|
| 3438 |
``` cpp
|
| 3439 |
alignas(T) alignas(A) T buffer[N];
|
| 3440 |
```
|
| 3441 |
|
| 3442 |
Specifying `alignas(T)` ensures that the final requested alignment will
|
| 3443 |
not be weaker than `alignof(T)`, and therefore the program will not be
|
| 3444 |
ill-formed.
|
| 3445 |
|
| 3446 |
+
— *end example*]
|
| 3447 |
+
|
| 3448 |
+
[*Example 4*:
|
| 3449 |
+
|
| 3450 |
``` cpp
|
| 3451 |
alignas(double) void f(); // error: alignment applied to function
|
| 3452 |
alignas(double) unsigned char c[sizeof(double)]; // array of characters, suitably aligned for a double
|
| 3453 |
extern unsigned char c[sizeof(double)]; // no alignas necessary
|
| 3454 |
alignas(float)
|
| 3455 |
extern unsigned char c[sizeof(double)]; // error: different alignment in declaration
|
| 3456 |
```
|
| 3457 |
|
| 3458 |
+
— *end example*]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3459 |
|
| 3460 |
### Carries dependency attribute <a id="dcl.attr.depend">[[dcl.attr.depend]]</a>
|
| 3461 |
|
| 3462 |
The *attribute-token* `carries_dependency` specifies dependency
|
| 3463 |
propagation into and out of functions. It shall appear at most once in
|
|
|
|
| 3480 |
attribute for that parameter. If a function or one of its parameters is
|
| 3481 |
declared with the `carries_dependency` attribute in its first
|
| 3482 |
declaration in one translation unit and the same function or one of its
|
| 3483 |
parameters is declared without the `carries_dependency` attribute in its
|
| 3484 |
first declaration in another translation unit, the program is
|
| 3485 |
+
ill-formed, no diagnostic required.
|
| 3486 |
|
| 3487 |
+
[*Note 1*: The `carries_dependency` attribute does not change the
|
| 3488 |
+
meaning of the program, but may result in generation of more efficient
|
| 3489 |
+
code. — *end note*]
|
| 3490 |
+
|
| 3491 |
+
[*Example 1*:
|
| 3492 |
|
| 3493 |
``` cpp
|
| 3494 |
/* Translation unit A. */
|
| 3495 |
|
| 3496 |
struct foo { int* a; int* b; };
|
|
|
|
| 3531 |
but its first parameter does not. Therefore, function `h`’s first call
|
| 3532 |
to `g` carries a dependency into `g`, but its second call does not. The
|
| 3533 |
implementation might need to insert a fence prior to the second call to
|
| 3534 |
`g`.
|
| 3535 |
|
| 3536 |
+
— *end example*]
|
| 3537 |
+
|
| 3538 |
### Deprecated attribute <a id="dcl.attr.deprecated">[[dcl.attr.deprecated]]</a>
|
| 3539 |
|
| 3540 |
The *attribute-token* `deprecated` can be used to mark names and
|
| 3541 |
entities whose use is still allowed, but is discouraged for some reason.
|
| 3542 |
+
|
| 3543 |
+
[*Note 1*: In particular, `deprecated` is appropriate for names and
|
| 3544 |
+
entities that are deemed obsolescent or unsafe. — *end note*]
|
| 3545 |
+
|
| 3546 |
+
It shall appear at most once in each *attribute-list*. An
|
| 3547 |
+
*attribute-argument-clause* may be present and, if present, it shall
|
| 3548 |
+
have the form:
|
| 3549 |
|
| 3550 |
``` cpp
|
| 3551 |
( string-literal )
|
| 3552 |
```
|
| 3553 |
|
| 3554 |
+
[*Note 2*: The *string-literal* in the *attribute-argument-clause*
|
| 3555 |
+
could be used to explain the rationale for deprecation and/or to suggest
|
| 3556 |
+
a replacing entity. — *end note*]
|
| 3557 |
|
| 3558 |
The attribute may be applied to the declaration of a class, a
|
| 3559 |
+
*typedef-name*, a variable, a non-static data member, a function, a
|
| 3560 |
+
namespace, an enumeration, an enumerator, or a template specialization.
|
| 3561 |
|
| 3562 |
A name or entity declared without the `deprecated` attribute can later
|
| 3563 |
+
be redeclared with the attribute and vice-versa.
|
| 3564 |
+
|
| 3565 |
+
[*Note 3*: Thus, an entity initially declared without the attribute can
|
| 3566 |
+
be marked as deprecated by a subsequent redeclaration. However, after an
|
| 3567 |
+
entity is marked as deprecated, later redeclarations do not un-deprecate
|
| 3568 |
+
the entity. — *end note*]
|
| 3569 |
+
|
| 3570 |
Redeclarations using different forms of the attribute (with or without
|
| 3571 |
the *attribute-argument-clause* or with different
|
| 3572 |
*attribute-argument-clause*s) are allowed.
|
| 3573 |
|
| 3574 |
+
[*Note 4*: Implementations may use the `deprecated` attribute to
|
| 3575 |
+
produce a diagnostic message in case the program refers to a name or
|
| 3576 |
+
entity other than to declare it, after a declaration that specifies the
|
| 3577 |
+
attribute. The diagnostic message may include the text provided within
|
| 3578 |
+
the *attribute-argument-clause* of any `deprecated` attribute applied to
|
| 3579 |
+
the name or entity. — *end note*]
|
| 3580 |
+
|
| 3581 |
+
### Fallthrough attribute <a id="dcl.attr.fallthrough">[[dcl.attr.fallthrough]]</a>
|
| 3582 |
+
|
| 3583 |
+
The *attribute-token* `fallthrough` may be applied to a null statement (
|
| 3584 |
+
[[stmt.expr]]); such a statement is a fallthrough statement. The
|
| 3585 |
+
*attribute-token* `fallthrough` shall appear at most once in each
|
| 3586 |
+
*attribute-list* and no *attribute-argument-clause* shall be present. A
|
| 3587 |
+
fallthrough statement may only appear within an enclosing `switch`
|
| 3588 |
+
statement ([[stmt.switch]]). The next statement that would be executed
|
| 3589 |
+
after a fallthrough statement shall be a labeled statement whose label
|
| 3590 |
+
is a case label or default label for the same `switch` statement. The
|
| 3591 |
+
program is ill-formed if there is no such statement.
|
| 3592 |
+
|
| 3593 |
+
[*Note 1*: The use of a fallthrough statement is intended to suppress a
|
| 3594 |
+
warning that an implementation might otherwise issue for a case or
|
| 3595 |
+
default label that is reachable from another case or default label along
|
| 3596 |
+
some path of execution. Implementations are encouraged to issue a
|
| 3597 |
+
warning if a fallthrough statement is not dynamically
|
| 3598 |
+
reachable. — *end note*]
|
| 3599 |
+
|
| 3600 |
+
[*Example 1*:
|
| 3601 |
+
|
| 3602 |
+
``` cpp
|
| 3603 |
+
void f(int n) {
|
| 3604 |
+
void g(), h(), i();
|
| 3605 |
+
switch (n) {
|
| 3606 |
+
case 1:
|
| 3607 |
+
case 2:
|
| 3608 |
+
g();
|
| 3609 |
+
[[fallthrough]];
|
| 3610 |
+
case 3: // warning on fallthrough discouraged
|
| 3611 |
+
h();
|
| 3612 |
+
case 4: // implementation may warn on fallthrough
|
| 3613 |
+
i();
|
| 3614 |
+
[[fallthrough]]; // ill-formed
|
| 3615 |
+
}
|
| 3616 |
+
}
|
| 3617 |
+
```
|
| 3618 |
+
|
| 3619 |
+
— *end example*]
|
| 3620 |
+
|
| 3621 |
+
### Maybe unused attribute <a id="dcl.attr.unused">[[dcl.attr.unused]]</a>
|
| 3622 |
+
|
| 3623 |
+
The *attribute-token* `maybe_unused` indicates that a name or entity is
|
| 3624 |
+
possibly intentionally unused. It shall appear at most once in each
|
| 3625 |
+
*attribute-list* and no *attribute-argument-clause* shall be present.
|
| 3626 |
+
|
| 3627 |
+
The attribute may be applied to the declaration of a class, a
|
| 3628 |
+
*typedef-name*, a variable, a non-static data member, a function, an
|
| 3629 |
+
enumeration, or an enumerator.
|
| 3630 |
+
|
| 3631 |
+
[*Note 1*: For an entity marked `maybe_unused`, implementations are
|
| 3632 |
+
encouraged not to emit a warning that the entity is unused, or that the
|
| 3633 |
+
entity is used despite the presence of the attribute. — *end note*]
|
| 3634 |
+
|
| 3635 |
+
A name or entity declared without the `maybe_unused` attribute can later
|
| 3636 |
+
be redeclared with the attribute and vice versa. An entity is considered
|
| 3637 |
+
marked after the first declaration that marks it.
|
| 3638 |
+
|
| 3639 |
+
[*Example 1*:
|
| 3640 |
+
|
| 3641 |
+
``` cpp
|
| 3642 |
+
[[maybe_unused]] void f([[maybe_unused]] bool thing1,
|
| 3643 |
+
[[maybe_unused]] bool thing2) {
|
| 3644 |
+
[[maybe_unused]] bool b = thing1 && thing2;
|
| 3645 |
+
assert(b);
|
| 3646 |
+
}
|
| 3647 |
+
```
|
| 3648 |
+
|
| 3649 |
+
Implementations are encouraged not to warn that `b` is unused, whether
|
| 3650 |
+
or not `NDEBUG` is defined.
|
| 3651 |
+
|
| 3652 |
+
— *end example*]
|
| 3653 |
+
|
| 3654 |
+
### Nodiscard attribute <a id="dcl.attr.nodiscard">[[dcl.attr.nodiscard]]</a>
|
| 3655 |
+
|
| 3656 |
+
The *attribute-token* `nodiscard` may be applied to the *declarator-id*
|
| 3657 |
+
in a function declaration or to the declaration of a class or
|
| 3658 |
+
enumeration. It shall appear at most once in each *attribute-list* and
|
| 3659 |
+
no *attribute-argument-clause* shall be present.
|
| 3660 |
+
|
| 3661 |
+
[*Note 1*: A nodiscard call is a function call expression that calls a
|
| 3662 |
+
function previously declared `nodiscard`, or whose return type is a
|
| 3663 |
+
possibly cv-qualified class or enumeration type marked `nodiscard`.
|
| 3664 |
+
Appearance of a nodiscard call as a potentially-evaluated
|
| 3665 |
+
discarded-value expression (Clause [[expr]]) is discouraged unless
|
| 3666 |
+
explicitly cast to `void`. Implementations are encouraged to issue a
|
| 3667 |
+
warning in such cases. This is typically because discarding the return
|
| 3668 |
+
value of a nodiscard call has surprising consequences. — *end note*]
|
| 3669 |
+
|
| 3670 |
+
[*Example 1*:
|
| 3671 |
+
|
| 3672 |
+
``` cpp
|
| 3673 |
+
struct [[nodiscard]] error_info { ... };
|
| 3674 |
+
error_info enable_missile_safety_mode();
|
| 3675 |
+
void launch_missiles();
|
| 3676 |
+
void test_missiles() {
|
| 3677 |
+
enable_missile_safety_mode(); // warning encouraged
|
| 3678 |
+
launch_missiles();
|
| 3679 |
+
}
|
| 3680 |
+
error_info &foo();
|
| 3681 |
+
void f() { foo(); } // warning not encouraged: not a nodiscard call, because neither
|
| 3682 |
+
// the (reference) return type nor the function is declared nodiscard
|
| 3683 |
+
```
|
| 3684 |
+
|
| 3685 |
+
— *end example*]
|
| 3686 |
+
|
| 3687 |
+
### Noreturn attribute <a id="dcl.attr.noreturn">[[dcl.attr.noreturn]]</a>
|
| 3688 |
+
|
| 3689 |
+
The *attribute-token* `noreturn` specifies that a function does not
|
| 3690 |
+
return. It shall appear at most once in each *attribute-list* and no
|
| 3691 |
+
*attribute-argument-clause* shall be present. The attribute may be
|
| 3692 |
+
applied to the *declarator-id* in a function declaration. The first
|
| 3693 |
+
declaration of a function shall specify the `noreturn` attribute if any
|
| 3694 |
+
declaration of that function specifies the `noreturn` attribute. If a
|
| 3695 |
+
function is declared with the `noreturn` attribute in one translation
|
| 3696 |
+
unit and the same function is declared without the `noreturn` attribute
|
| 3697 |
+
in another translation unit, the program is ill-formed, no diagnostic
|
| 3698 |
+
required.
|
| 3699 |
+
|
| 3700 |
+
If a function `f` is called where `f` was previously declared with the
|
| 3701 |
+
`noreturn` attribute and `f` eventually returns, the behavior is
|
| 3702 |
+
undefined.
|
| 3703 |
+
|
| 3704 |
+
[*Note 1*: The function may terminate by throwing an
|
| 3705 |
+
exception. — *end note*]
|
| 3706 |
+
|
| 3707 |
+
[*Note 2*: Implementations are encouraged to issue a warning if a
|
| 3708 |
+
function marked `[[noreturn]]` might return. — *end note*]
|
| 3709 |
+
|
| 3710 |
+
[*Example 1*:
|
| 3711 |
+
|
| 3712 |
+
``` cpp
|
| 3713 |
+
[[ noreturn ]] void f() {
|
| 3714 |
+
throw "error"; // OK
|
| 3715 |
+
}
|
| 3716 |
+
|
| 3717 |
+
[[ noreturn ]] void q(int i) { // behavior is undefined if called with an argument <= 0
|
| 3718 |
+
if (i > 0)
|
| 3719 |
+
throw "positive";
|
| 3720 |
+
}
|
| 3721 |
+
```
|
| 3722 |
+
|
| 3723 |
+
— *end example*]
|
| 3724 |
|