- tmp/tmpt4t8ral9/{from.md → to.md} +413 -202
tmp/tmpt4t8ral9/{from.md → to.md}
RENAMED
|
@@ -9,72 +9,67 @@ more translation units.
|
|
| 9 |
The outermost declarative region of a translation unit is a namespace;
|
| 10 |
see [[basic.scope.namespace]].
|
| 11 |
|
| 12 |
### Namespace definition <a id="namespace.def">[[namespace.def]]</a>
|
| 13 |
|
| 14 |
-
The grammar for a *namespace-definition* is
|
| 15 |
-
|
| 16 |
``` bnf
|
| 17 |
namespace-name:
|
| 18 |
-
|
| 19 |
namespace-alias
|
| 20 |
```
|
| 21 |
|
| 22 |
-
``` bnf
|
| 23 |
-
original-namespace-name:
|
| 24 |
-
identifier
|
| 25 |
-
```
|
| 26 |
-
|
| 27 |
``` bnf
|
| 28 |
namespace-definition:
|
| 29 |
named-namespace-definition
|
| 30 |
unnamed-namespace-definition
|
|
|
|
| 31 |
```
|
| 32 |
|
| 33 |
``` bnf
|
| 34 |
named-namespace-definition:
|
| 35 |
-
|
| 36 |
-
extension-namespace-definition
|
| 37 |
-
```
|
| 38 |
-
|
| 39 |
-
``` bnf
|
| 40 |
-
original-namespace-definition:
|
| 41 |
-
'inline'ₒₚₜ 'namespace' identifier '{' namespace-body '}'
|
| 42 |
-
```
|
| 43 |
-
|
| 44 |
-
``` bnf
|
| 45 |
-
extension-namespace-definition:
|
| 46 |
-
'inline'ₒₚₜ 'namespace' original-namespace-name '{' namespace-body '}'
|
| 47 |
```
|
| 48 |
|
| 49 |
``` bnf
|
| 50 |
unnamed-namespace-definition:
|
| 51 |
-
'inline'ₒₚₜ 'namespace {' namespace-body '}'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
```
|
| 53 |
|
| 54 |
``` bnf
|
| 55 |
namespace-body:
|
| 56 |
declaration-seqₒₚₜ
|
| 57 |
```
|
| 58 |
|
| 59 |
-
The *identifier* in an *original-namespace-definition* shall not have
|
| 60 |
-
been previously defined in the declarative region in which the
|
| 61 |
-
*original-namespace-definition* appears. The *identifier* in an
|
| 62 |
-
*original-namespace-definition* is the name of the namespace.
|
| 63 |
-
Subsequently in that declarative region, it is treated as an
|
| 64 |
-
*original-namespace-name*.
|
| 65 |
-
|
| 66 |
-
The *original-namespace-name* in an *extension-namespace-definition*
|
| 67 |
-
shall have previously been defined in an *original-namespace-definition*
|
| 68 |
-
in the same declarative region.
|
| 69 |
-
|
| 70 |
Every *namespace-definition* shall appear in the global scope or in a
|
| 71 |
namespace scope ([[basic.scope.namespace]]).
|
| 72 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
Because a *namespace-definition* contains *declaration*s in its
|
| 74 |
*namespace-body* and a *namespace-definition* is itself a *declaration*,
|
| 75 |
-
it follows that *namespace-
|
|
|
|
|
|
|
| 76 |
|
| 77 |
``` cpp
|
| 78 |
namespace Outer {
|
| 79 |
int i;
|
| 80 |
namespace Inner {
|
|
@@ -83,16 +78,20 @@ namespace Outer {
|
|
| 83 |
void g() { i++; } // Inner::i
|
| 84 |
}
|
| 85 |
}
|
| 86 |
```
|
| 87 |
|
|
|
|
|
|
|
| 88 |
The *enclosing namespaces* of a declaration are those namespaces in
|
| 89 |
which the declaration lexically appears, except for a redeclaration of a
|
| 90 |
namespace member outside its original namespace (e.g., a definition as
|
| 91 |
specified in [[namespace.memdef]]). Such a redeclaration has the same
|
| 92 |
enclosing namespaces as the original declaration.
|
| 93 |
|
|
|
|
|
|
|
| 94 |
``` cpp
|
| 95 |
namespace Q {
|
| 96 |
namespace V {
|
| 97 |
void f(); // enclosing namespaces are the global namespace, Q, and Q::V
|
| 98 |
class C { void m(); };
|
|
@@ -103,55 +102,94 @@ namespace Q {
|
|
| 103 |
void V::C::m() { // enclosing namespaces are the global namespace, Q, and Q::V
|
| 104 |
}
|
| 105 |
}
|
| 106 |
```
|
| 107 |
|
|
|
|
|
|
|
| 108 |
If the optional initial `inline` keyword appears in a
|
| 109 |
*namespace-definition* for a particular namespace, that namespace is
|
| 110 |
declared to be an *inline namespace*. The `inline` keyword may be used
|
| 111 |
-
on
|
| 112 |
-
the *
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
|
| 114 |
Members of an inline namespace can be used in most respects as though
|
| 115 |
they were members of the enclosing namespace. Specifically, the inline
|
| 116 |
namespace and its enclosing namespace are both added to the set of
|
| 117 |
associated namespaces used in argument-dependent lookup (
|
| 118 |
[[basic.lookup.argdep]]) whenever one of them is, and a
|
| 119 |
*using-directive* ([[namespace.udir]]) that names the inline namespace
|
| 120 |
is implicitly inserted into the enclosing namespace as for an unnamed
|
| 121 |
namespace ([[namespace.unnamed]]). Furthermore, each member of the
|
| 122 |
-
inline namespace can subsequently be
|
| 123 |
-
[[temp.
|
| 124 |
-
though it were a member
|
| 125 |
-
|
| 126 |
-
[[namespace.qual]]) will include
|
| 127 |
-
in by the *using-directive* even
|
| 128 |
-
in the enclosing namespace.
|
| 129 |
|
| 130 |
These properties are transitive: if a namespace `N` contains an inline
|
| 131 |
namespace `M`, which in turn contains an inline namespace `O`, then the
|
| 132 |
members of `O` can be used as though they were members of `M` or `N`.
|
| 133 |
The *inline namespace set* of `N` is the transitive closure of all
|
| 134 |
inline namespaces in `N`. The *enclosing namespace set* of `O` is the
|
| 135 |
set of namespaces consisting of the innermost non-inline namespace
|
| 136 |
enclosing an inline namespace `O`, together with any intervening inline
|
| 137 |
namespaces.
|
| 138 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 139 |
#### Unnamed namespaces <a id="namespace.unnamed">[[namespace.unnamed]]</a>
|
| 140 |
|
| 141 |
An *unnamed-namespace-definition* behaves as if it were replaced by
|
| 142 |
|
| 143 |
``` bnf
|
| 144 |
-
'inline'ₒₚₜ 'namespace' unique '{ /* empty body */ }'
|
| 145 |
-
'using namespace' unique ';'
|
| 146 |
-
'namespace' unique '{' namespace-body '}'
|
| 147 |
```
|
| 148 |
|
| 149 |
where `inline` appears if and only if it appears in the
|
| 150 |
-
*unnamed-namespace-definition*
|
| 151 |
translation unit are replaced by the same identifier, and this
|
| 152 |
-
identifier differs from all other identifiers in the
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
|
| 154 |
``` cpp
|
| 155 |
namespace { int i; } // unique ::i
|
| 156 |
void f() { i++; } // unique ::i++
|
| 157 |
|
|
@@ -169,64 +207,95 @@ void h() {
|
|
| 169 |
A::i++; // A::unique ::i
|
| 170 |
j++; // A::unique ::j
|
| 171 |
}
|
| 172 |
```
|
| 173 |
|
|
|
|
|
|
|
| 174 |
#### Namespace member definitions <a id="namespace.memdef">[[namespace.memdef]]</a>
|
| 175 |
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 179 |
|
| 180 |
``` cpp
|
| 181 |
namespace X {
|
| 182 |
-
void f() {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 183 |
}
|
| 184 |
```
|
| 185 |
|
|
|
|
|
|
|
| 186 |
Members of a named namespace can also be defined outside that namespace
|
| 187 |
by explicit qualification ([[namespace.qual]]) of the name being
|
| 188 |
defined, provided that the entity being defined was already declared in
|
| 189 |
the namespace and the definition appears after the point of declaration
|
| 190 |
in a namespace that encloses the declaration’s namespace.
|
| 191 |
|
|
|
|
|
|
|
| 192 |
``` cpp
|
| 193 |
namespace Q {
|
| 194 |
namespace V {
|
| 195 |
void f();
|
| 196 |
}
|
| 197 |
-
void V::f() {
|
| 198 |
-
void V::g() {
|
| 199 |
namespace V {
|
| 200 |
void g();
|
| 201 |
}
|
| 202 |
}
|
| 203 |
|
| 204 |
namespace R {
|
| 205 |
-
void Q::V::g() {
|
| 206 |
}
|
| 207 |
```
|
| 208 |
|
| 209 |
-
|
|
|
|
| 210 |
If a `friend` declaration in a non-local class first declares a class,
|
| 211 |
-
function, class template or function template[^
|
| 212 |
of the innermost enclosing namespace. The `friend` declaration does not
|
| 213 |
by itself make the name visible to unqualified lookup (
|
| 214 |
[[basic.lookup.unqual]]) or qualified lookup ([[basic.lookup.qual]]).
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
lookup
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 228 |
|
| 229 |
``` cpp
|
| 230 |
// Assume f and g have not yet been declared.
|
| 231 |
void h(int);
|
| 232 |
template <class T> void f2(T);
|
|
@@ -242,12 +311,12 @@ namespace A {
|
|
| 242 |
};
|
| 243 |
|
| 244 |
// A::f, A::g and A::h are not visible here
|
| 245 |
X x;
|
| 246 |
void g() { f(x); } // definition of A::g
|
| 247 |
-
void f(X) {
|
| 248 |
-
void h(int) {
|
| 249 |
// A::f, A::g and A::h are visible here and known to be friends
|
| 250 |
}
|
| 251 |
|
| 252 |
using A::x;
|
| 253 |
|
|
@@ -256,10 +325,12 @@ void h() {
|
|
| 256 |
A::X::f(x); // error: f is not a member of A::X
|
| 257 |
A::X::Y::g(); // error: g is not a member of A::X::Y
|
| 258 |
}
|
| 259 |
```
|
| 260 |
|
|
|
|
|
|
|
| 261 |
### Namespace alias <a id="namespace.alias">[[namespace.alias]]</a>
|
| 262 |
|
| 263 |
A *namespace-alias-definition* declares an alternate name for a
|
| 264 |
namespace according to the following grammar:
|
| 265 |
|
|
@@ -278,57 +349,75 @@ qualified-namespace-specifier:
|
|
| 278 |
nested-name-specifierₒₚₜ namespace-name
|
| 279 |
```
|
| 280 |
|
| 281 |
The *identifier* in a *namespace-alias-definition* is a synonym for the
|
| 282 |
name of the namespace denoted by the *qualified-namespace-specifier* and
|
| 283 |
-
becomes a *namespace-alias*.
|
|
|
|
|
|
|
| 284 |
*namespace-alias-definition*, only namespace names are considered, see
|
| 285 |
-
[[basic.lookup.udir]].
|
| 286 |
|
| 287 |
In a declarative region, a *namespace-alias-definition* can be used to
|
| 288 |
redefine a *namespace-alias* declared in that declarative region to
|
| 289 |
-
refer only to the namespace to which it already refers.
|
| 290 |
-
|
|
|
|
|
|
|
|
|
|
| 291 |
|
| 292 |
``` cpp
|
| 293 |
-
namespace Company_with_very_long_name {
|
| 294 |
namespace CWVLN = Company_with_very_long_name;
|
| 295 |
namespace CWVLN = Company_with_very_long_name; // OK: duplicate
|
| 296 |
namespace CWVLN = CWVLN;
|
| 297 |
```
|
| 298 |
|
| 299 |
-
|
| 300 |
-
name of any other entity in the same declarative region. A
|
| 301 |
-
*namespace-name* defined at global scope shall not be declared as the
|
| 302 |
-
name of any other entity in any global scope of the program. No
|
| 303 |
-
diagnostic is required for a violation of this rule by declarations in
|
| 304 |
-
different translation units.
|
| 305 |
|
| 306 |
### The `using` declaration <a id="namespace.udecl">[[namespace.udecl]]</a>
|
| 307 |
|
| 308 |
-
A *using-declaration* introduces a name into the declarative region in
|
| 309 |
-
which the *using-declaration* appears.
|
| 310 |
-
|
| 311 |
``` bnf
|
| 312 |
using-declaration:
|
| 313 |
-
'using
|
| 314 |
-
'using ::' unqualified-id ';'
|
| 315 |
```
|
| 316 |
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 327 |
|
| 328 |
Every *using-declaration* is a *declaration* and a *member-declaration*
|
| 329 |
-
and
|
|
|
|
|
|
|
| 330 |
|
| 331 |
``` cpp
|
| 332 |
struct B {
|
| 333 |
void f(char);
|
| 334 |
void g(char);
|
|
@@ -341,16 +430,32 @@ struct D : B {
|
|
| 341 |
void f(int) { f('c'); } // calls B::f(char)
|
| 342 |
void g(int) { g('c'); } // recursively calls D::g(int)
|
| 343 |
};
|
| 344 |
```
|
| 345 |
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
*nested-name-specifier* shall name a
|
| 350 |
-
being defined
|
| 351 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 352 |
|
| 353 |
``` cpp
|
| 354 |
class C {
|
| 355 |
int g();
|
| 356 |
};
|
|
@@ -361,25 +466,30 @@ class D2 : public B {
|
|
| 361 |
using B::x; // OK: x is a union member of base B
|
| 362 |
using C::g; // error: C isn't a base of D2
|
| 363 |
};
|
| 364 |
```
|
| 365 |
|
| 366 |
-
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 373 |
*using-declaration* does not by itself suppress the implicit declaration
|
| 374 |
-
of the derived class
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
as described below.
|
| 378 |
|
| 379 |
A *using-declaration* shall not name a *template-id*.
|
| 380 |
|
|
|
|
|
|
|
| 381 |
``` cpp
|
| 382 |
struct A {
|
| 383 |
template <class T> void f(T);
|
| 384 |
template <class T> struct X { };
|
| 385 |
};
|
|
@@ -387,34 +497,39 @@ struct B : A {
|
|
| 387 |
using A::f<double>; // ill-formed
|
| 388 |
using A::X<int>; // ill-formed
|
| 389 |
};
|
| 390 |
```
|
| 391 |
|
|
|
|
|
|
|
| 392 |
A *using-declaration* shall not name a namespace.
|
| 393 |
|
| 394 |
A *using-declaration* shall not name a scoped enumerator.
|
| 395 |
|
| 396 |
-
A *using-declaration*
|
| 397 |
*member-declaration*.
|
| 398 |
|
|
|
|
|
|
|
| 399 |
``` cpp
|
| 400 |
struct X {
|
| 401 |
int i;
|
| 402 |
static int s;
|
| 403 |
};
|
| 404 |
|
| 405 |
void f() {
|
| 406 |
-
using X::i;
|
| 407 |
-
|
| 408 |
-
using X::s; // error: X::s is a class member
|
| 409 |
-
// and this is not a member declaration.
|
| 410 |
}
|
| 411 |
```
|
| 412 |
|
|
|
|
|
|
|
| 413 |
Members declared by a *using-declaration* can be referred to by explicit
|
| 414 |
-
qualification just like other member names ([[namespace.qual]]).
|
| 415 |
-
|
|
|
|
| 416 |
|
| 417 |
``` cpp
|
| 418 |
void f();
|
| 419 |
|
| 420 |
namespace A {
|
|
@@ -431,78 +546,83 @@ void h()
|
|
| 431 |
X::f(); // calls ::f
|
| 432 |
X::g(); // calls A::g
|
| 433 |
}
|
| 434 |
```
|
| 435 |
|
|
|
|
|
|
|
| 436 |
A *using-declaration* is a *declaration* and can therefore be used
|
| 437 |
repeatedly where (and only where) multiple declarations are allowed.
|
| 438 |
|
|
|
|
|
|
|
| 439 |
``` cpp
|
| 440 |
namespace A {
|
| 441 |
int i;
|
| 442 |
}
|
| 443 |
|
| 444 |
namespace A1 {
|
| 445 |
-
using A::i;
|
| 446 |
-
using A::i; // OK: double declaration
|
| 447 |
-
}
|
| 448 |
-
|
| 449 |
-
void f() {
|
| 450 |
-
using A::i;
|
| 451 |
-
using A::i; // error: double declaration
|
| 452 |
}
|
| 453 |
|
| 454 |
struct B {
|
| 455 |
int i;
|
| 456 |
};
|
| 457 |
|
| 458 |
struct X : B {
|
| 459 |
-
using B::i;
|
| 460 |
-
using B::i; // error: double member declaration
|
| 461 |
};
|
| 462 |
```
|
| 463 |
|
| 464 |
-
|
| 465 |
-
|
| 466 |
-
|
| 467 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 468 |
[[temp.param]]), and template specializations ([[temp.class.spec]],
|
| 469 |
-
[[temp.expl.spec]]) are considered.
|
|
|
|
|
|
|
| 470 |
|
| 471 |
``` cpp
|
| 472 |
namespace A {
|
| 473 |
void f(int);
|
| 474 |
}
|
| 475 |
|
| 476 |
-
using A::f; // f is a synonym for A::f;
|
| 477 |
-
// that is, for A::f(int).
|
| 478 |
namespace A {
|
| 479 |
void f(char);
|
| 480 |
}
|
| 481 |
|
| 482 |
void foo() {
|
| 483 |
-
f('a'); // calls f(int),
|
| 484 |
-
}
|
| 485 |
|
| 486 |
void bar() {
|
| 487 |
-
using A::f; // f is a synonym for A::f;
|
| 488 |
-
// that is, for A::f(int) and A::f(char).
|
| 489 |
f('a'); // calls f(char)
|
| 490 |
}
|
| 491 |
```
|
| 492 |
|
| 493 |
-
|
| 494 |
-
|
| 495 |
-
|
| 496 |
-
|
| 497 |
-
|
| 498 |
-
|
|
|
|
|
|
|
| 499 |
|
| 500 |
Since a *using-declaration* is a declaration, the restrictions on
|
| 501 |
declarations of the same name in the same declarative region (
|
| 502 |
[[basic.scope]]) also apply to *using-declaration*s.
|
| 503 |
|
|
|
|
|
|
|
| 504 |
``` cpp
|
| 505 |
namespace A {
|
| 506 |
int x;
|
| 507 |
}
|
| 508 |
|
|
@@ -529,22 +649,29 @@ void func() {
|
|
| 529 |
x = 99; // assigns to A::x
|
| 530 |
struct x x1; // x1 has class type B::x
|
| 531 |
}
|
| 532 |
```
|
| 533 |
|
|
|
|
|
|
|
| 534 |
If a function declaration in namespace scope or block scope has the same
|
| 535 |
name and the same parameter-type-list ([[dcl.fct]]) as a function
|
| 536 |
introduced by a *using-declaration*, and the declarations do not declare
|
| 537 |
the same function, the program is ill-formed. If a function template
|
| 538 |
declaration in namespace scope has the same name, parameter-type-list,
|
| 539 |
return type, and template parameter list as a function template
|
| 540 |
-
introduced by a *using-declaration*, the program is ill-formed.
|
| 541 |
-
|
| 542 |
-
|
|
|
|
|
|
|
|
|
|
| 543 |
name, function overload resolution selects the functions introduced by
|
| 544 |
such *using-declaration*s, the function call is ill-formed.
|
| 545 |
|
|
|
|
|
|
|
| 546 |
``` cpp
|
| 547 |
namespace B {
|
| 548 |
void f(int);
|
| 549 |
void f(double);
|
| 550 |
}
|
|
@@ -561,17 +688,23 @@ void h() {
|
|
| 561 |
f(1); // error: ambiguous: B::f(int) or C::f(int)?
|
| 562 |
void f(int); // error: f(int) conflicts with C::f(int) and B::f(int)
|
| 563 |
}
|
| 564 |
```
|
| 565 |
|
| 566 |
-
|
| 567 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 568 |
derived class override and/or hide member functions and member function
|
| 569 |
templates with the same name, parameter-type-list ([[dcl.fct]]),
|
| 570 |
cv-qualification, and *ref-qualifier* (if any) in a base class (rather
|
| 571 |
-
than conflicting).
|
| 572 |
-
|
|
|
|
|
|
|
| 573 |
|
| 574 |
``` cpp
|
| 575 |
struct B {
|
| 576 |
virtual void f(int);
|
| 577 |
virtual void f(char);
|
|
@@ -595,32 +728,70 @@ void k(D* p)
|
|
| 595 |
p->f(1); // calls D::f(int)
|
| 596 |
p->f('a'); // calls B::f(char)
|
| 597 |
p->g(1); // calls B::g(int)
|
| 598 |
p->g('a'); // calls D::g(char)
|
| 599 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 600 |
```
|
| 601 |
|
| 602 |
-
|
| 603 |
-
|
| 604 |
-
|
|
|
|
|
|
|
| 605 |
implicit `this` parameter shall be treated as if it were a pointer to
|
| 606 |
the derived class rather than to the base class. This has no effect on
|
| 607 |
the type of the function, and in all other respects the function remains
|
| 608 |
-
a member of the base class.
|
| 609 |
-
|
| 610 |
-
|
| 611 |
-
[[class.
|
| 612 |
-
|
| 613 |
-
|
| 614 |
-
|
| 615 |
-
|
| 616 |
-
class
|
| 617 |
-
|
| 618 |
-
*using-
|
| 619 |
-
|
| 620 |
-
|
| 621 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 622 |
|
| 623 |
``` cpp
|
| 624 |
struct A { int x(); };
|
| 625 |
struct B : A { };
|
| 626 |
struct C : A {
|
|
@@ -631,18 +802,26 @@ struct C : A {
|
|
| 631 |
struct D : B, C {
|
| 632 |
using C::x;
|
| 633 |
int x(double);
|
| 634 |
};
|
| 635 |
int f(D* d) {
|
| 636 |
-
return d->x(); //
|
| 637 |
}
|
| 638 |
```
|
| 639 |
|
| 640 |
-
|
| 641 |
-
|
| 642 |
-
|
| 643 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 644 |
|
| 645 |
``` cpp
|
| 646 |
class A {
|
| 647 |
private:
|
| 648 |
void f(char);
|
|
@@ -657,11 +836,13 @@ class B : public A {
|
|
| 657 |
public:
|
| 658 |
using A::g; // B::g is a public synonym for A::g
|
| 659 |
};
|
| 660 |
```
|
| 661 |
|
| 662 |
-
|
|
|
|
|
|
|
| 663 |
dependent name ([[temp.dep]]), the name introduced by the
|
| 664 |
*using-declaration* is treated as a *typedef-name* ([[dcl.typedef]]).
|
| 665 |
|
| 666 |
### Using directive <a id="namespace.udir">[[namespace.udir]]</a>
|
| 667 |
|
|
@@ -669,26 +850,34 @@ dependent name ([[temp.dep]]), the name introduced by the
|
|
| 669 |
using-directive:
|
| 670 |
attribute-specifier-seqₒₚₜ 'using namespace' nested-name-specifierₒₚₜ namespace-name ';'
|
| 671 |
```
|
| 672 |
|
| 673 |
A *using-directive* shall not appear in class scope, but may appear in
|
| 674 |
-
namespace scope or in block scope.
|
| 675 |
-
|
| 676 |
-
[
|
| 677 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 678 |
|
| 679 |
A *using-directive* specifies that the names in the nominated namespace
|
| 680 |
can be used in the scope in which the *using-directive* appears after
|
| 681 |
the *using-directive*. During unqualified name lookup (
|
| 682 |
[[basic.lookup.unqual]]), the names appear as if they were declared in
|
| 683 |
the nearest enclosing namespace which contains both the
|
| 684 |
-
*using-directive* and the nominated namespace.
|
| 685 |
-
|
|
|
|
|
|
|
| 686 |
|
| 687 |
A *using-directive* does not add any members to the declarative region
|
| 688 |
in which it appears.
|
| 689 |
|
|
|
|
|
|
|
| 690 |
``` cpp
|
| 691 |
namespace A {
|
| 692 |
int i;
|
| 693 |
namespace B {
|
| 694 |
namespace C {
|
|
@@ -713,15 +902,22 @@ namespace A {
|
|
| 713 |
void f4() {
|
| 714 |
i = 5; // ill-formed; neither i is visible
|
| 715 |
}
|
| 716 |
```
|
| 717 |
|
|
|
|
|
|
|
| 718 |
For unqualified lookup ([[basic.lookup.unqual]]), the *using-directive*
|
| 719 |
is transitive: if a scope contains a *using-directive* that nominates a
|
| 720 |
second namespace that itself contains *using-directive*s, the effect is
|
| 721 |
as if the *using-directive*s from the second namespace also appeared in
|
| 722 |
-
the first.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 723 |
|
| 724 |
``` cpp
|
| 725 |
namespace M {
|
| 726 |
int i;
|
| 727 |
}
|
|
@@ -760,21 +956,27 @@ namespace B {
|
|
| 760 |
int n = j; // D::j hides B::j
|
| 761 |
}
|
| 762 |
}
|
| 763 |
```
|
| 764 |
|
| 765 |
-
|
| 766 |
-
|
| 767 |
-
|
| 768 |
-
*using-directive*
|
| 769 |
-
|
|
|
|
|
|
|
| 770 |
|
| 771 |
If name lookup finds a declaration for a name in two different
|
| 772 |
namespaces, and the declarations do not declare the same entity and do
|
| 773 |
-
not declare functions, the use of the name is ill-formed.
|
| 774 |
-
|
| 775 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 776 |
|
| 777 |
``` cpp
|
| 778 |
namespace A {
|
| 779 |
class X { };
|
| 780 |
extern "C" int g();
|
|
@@ -788,24 +990,31 @@ namespace B {
|
|
| 788 |
using namespace A;
|
| 789 |
using namespace B;
|
| 790 |
|
| 791 |
void f() {
|
| 792 |
X(1); // error: name X found in two namespaces
|
| 793 |
-
g(); //
|
| 794 |
-
h(); //
|
| 795 |
}
|
| 796 |
```
|
| 797 |
|
|
|
|
|
|
|
| 798 |
During overload resolution, all functions from the transitive search are
|
| 799 |
considered for argument matching. The set of declarations found by the
|
| 800 |
-
transitive search is unordered.
|
| 801 |
-
|
| 802 |
-
|
| 803 |
-
|
| 804 |
-
|
| 805 |
-
|
| 806 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 807 |
|
| 808 |
``` cpp
|
| 809 |
namespace D {
|
| 810 |
int d1;
|
| 811 |
void f(char);
|
|
@@ -834,5 +1043,7 @@ void f() {
|
|
| 834 |
f(1); // error: ambiguous: D::f(int) or E::f(int)?
|
| 835 |
f('a'); // OK: D::f(char)
|
| 836 |
}
|
| 837 |
```
|
| 838 |
|
|
|
|
|
|
|
|
|
| 9 |
The outermost declarative region of a translation unit is a namespace;
|
| 10 |
see [[basic.scope.namespace]].
|
| 11 |
|
| 12 |
### Namespace definition <a id="namespace.def">[[namespace.def]]</a>
|
| 13 |
|
|
|
|
|
|
|
| 14 |
``` bnf
|
| 15 |
namespace-name:
|
| 16 |
+
identifier
|
| 17 |
namespace-alias
|
| 18 |
```
|
| 19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
``` bnf
|
| 21 |
namespace-definition:
|
| 22 |
named-namespace-definition
|
| 23 |
unnamed-namespace-definition
|
| 24 |
+
nested-namespace-definition
|
| 25 |
```
|
| 26 |
|
| 27 |
``` bnf
|
| 28 |
named-namespace-definition:
|
| 29 |
+
'inline'ₒₚₜ 'namespace' attribute-specifier-seqₒₚₜ identifier '{' namespace-body '}'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
```
|
| 31 |
|
| 32 |
``` bnf
|
| 33 |
unnamed-namespace-definition:
|
| 34 |
+
'inline'ₒₚₜ 'namespace' attribute-specifier-seqₒₚₜ '{' namespace-body '}'
|
| 35 |
+
```
|
| 36 |
+
|
| 37 |
+
``` bnf
|
| 38 |
+
nested-namespace-definition:
|
| 39 |
+
'namespace' enclosing-namespace-specifier '::' identifier '{' namespace-body '}'
|
| 40 |
+
```
|
| 41 |
+
|
| 42 |
+
``` bnf
|
| 43 |
+
enclosing-namespace-specifier:
|
| 44 |
+
identifier
|
| 45 |
+
enclosing-namespace-specifier '::' identifier
|
| 46 |
```
|
| 47 |
|
| 48 |
``` bnf
|
| 49 |
namespace-body:
|
| 50 |
declaration-seqₒₚₜ
|
| 51 |
```
|
| 52 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
Every *namespace-definition* shall appear in the global scope or in a
|
| 54 |
namespace scope ([[basic.scope.namespace]]).
|
| 55 |
|
| 56 |
+
In a *named-namespace-definition*, the *identifier* is the name of the
|
| 57 |
+
namespace. If the *identifier*, when looked up (
|
| 58 |
+
[[basic.lookup.unqual]]), refers to a *namespace-name* (but not a
|
| 59 |
+
*namespace-alias*) that was introduced in the namespace in which the
|
| 60 |
+
*named-namespace-definition* appears or that was introduced in a member
|
| 61 |
+
of the inline namespace set of that namespace, the
|
| 62 |
+
*namespace-definition* *extends* the previously-declared namespace.
|
| 63 |
+
Otherwise, the *identifier* is introduced as a *namespace-name* into the
|
| 64 |
+
declarative region in which the *named-namespace-definition* appears.
|
| 65 |
+
|
| 66 |
Because a *namespace-definition* contains *declaration*s in its
|
| 67 |
*namespace-body* and a *namespace-definition* is itself a *declaration*,
|
| 68 |
+
it follows that *namespace-definition*s can be nested.
|
| 69 |
+
|
| 70 |
+
[*Example 1*:
|
| 71 |
|
| 72 |
``` cpp
|
| 73 |
namespace Outer {
|
| 74 |
int i;
|
| 75 |
namespace Inner {
|
|
|
|
| 78 |
void g() { i++; } // Inner::i
|
| 79 |
}
|
| 80 |
}
|
| 81 |
```
|
| 82 |
|
| 83 |
+
— *end example*]
|
| 84 |
+
|
| 85 |
The *enclosing namespaces* of a declaration are those namespaces in
|
| 86 |
which the declaration lexically appears, except for a redeclaration of a
|
| 87 |
namespace member outside its original namespace (e.g., a definition as
|
| 88 |
specified in [[namespace.memdef]]). Such a redeclaration has the same
|
| 89 |
enclosing namespaces as the original declaration.
|
| 90 |
|
| 91 |
+
[*Example 2*:
|
| 92 |
+
|
| 93 |
``` cpp
|
| 94 |
namespace Q {
|
| 95 |
namespace V {
|
| 96 |
void f(); // enclosing namespaces are the global namespace, Q, and Q::V
|
| 97 |
class C { void m(); };
|
|
|
|
| 102 |
void V::C::m() { // enclosing namespaces are the global namespace, Q, and Q::V
|
| 103 |
}
|
| 104 |
}
|
| 105 |
```
|
| 106 |
|
| 107 |
+
— *end example*]
|
| 108 |
+
|
| 109 |
If the optional initial `inline` keyword appears in a
|
| 110 |
*namespace-definition* for a particular namespace, that namespace is
|
| 111 |
declared to be an *inline namespace*. The `inline` keyword may be used
|
| 112 |
+
on a *namespace-definition* that extends a namespace only if it was
|
| 113 |
+
previously used on the *namespace-definition* that initially declared
|
| 114 |
+
the *namespace-name* for that namespace.
|
| 115 |
+
|
| 116 |
+
The optional *attribute-specifier-seq* in a *named-namespace-definition*
|
| 117 |
+
appertains to the namespace being defined or extended.
|
| 118 |
|
| 119 |
Members of an inline namespace can be used in most respects as though
|
| 120 |
they were members of the enclosing namespace. Specifically, the inline
|
| 121 |
namespace and its enclosing namespace are both added to the set of
|
| 122 |
associated namespaces used in argument-dependent lookup (
|
| 123 |
[[basic.lookup.argdep]]) whenever one of them is, and a
|
| 124 |
*using-directive* ([[namespace.udir]]) that names the inline namespace
|
| 125 |
is implicitly inserted into the enclosing namespace as for an unnamed
|
| 126 |
namespace ([[namespace.unnamed]]). Furthermore, each member of the
|
| 127 |
+
inline namespace can subsequently be partially specialized (
|
| 128 |
+
[[temp.class.spec]]), explicitly instantiated ([[temp.explicit]]), or
|
| 129 |
+
explicitly specialized ([[temp.expl.spec]]) as though it were a member
|
| 130 |
+
of the enclosing namespace. Finally, looking up a name in the enclosing
|
| 131 |
+
namespace via explicit qualification ([[namespace.qual]]) will include
|
| 132 |
+
members of the inline namespace brought in by the *using-directive* even
|
| 133 |
+
if there are declarations of that name in the enclosing namespace.
|
| 134 |
|
| 135 |
These properties are transitive: if a namespace `N` contains an inline
|
| 136 |
namespace `M`, which in turn contains an inline namespace `O`, then the
|
| 137 |
members of `O` can be used as though they were members of `M` or `N`.
|
| 138 |
The *inline namespace set* of `N` is the transitive closure of all
|
| 139 |
inline namespaces in `N`. The *enclosing namespace set* of `O` is the
|
| 140 |
set of namespaces consisting of the innermost non-inline namespace
|
| 141 |
enclosing an inline namespace `O`, together with any intervening inline
|
| 142 |
namespaces.
|
| 143 |
|
| 144 |
+
A *nested-namespace-definition* with an *enclosing-namespace-specifier*
|
| 145 |
+
`E`, *identifier* `I` and *namespace-body* `B` is equivalent to
|
| 146 |
+
|
| 147 |
+
``` cpp
|
| 148 |
+
namespace E { namespace I { B } }
|
| 149 |
+
```
|
| 150 |
+
|
| 151 |
+
[*Example 3*:
|
| 152 |
+
|
| 153 |
+
``` cpp
|
| 154 |
+
namespace A::B::C {
|
| 155 |
+
int i;
|
| 156 |
+
}
|
| 157 |
+
```
|
| 158 |
+
|
| 159 |
+
The above has the same effect as:
|
| 160 |
+
|
| 161 |
+
``` cpp
|
| 162 |
+
namespace A {
|
| 163 |
+
namespace B {
|
| 164 |
+
namespace C {
|
| 165 |
+
int i;
|
| 166 |
+
}
|
| 167 |
+
}
|
| 168 |
+
}
|
| 169 |
+
```
|
| 170 |
+
|
| 171 |
+
— *end example*]
|
| 172 |
+
|
| 173 |
#### Unnamed namespaces <a id="namespace.unnamed">[[namespace.unnamed]]</a>
|
| 174 |
|
| 175 |
An *unnamed-namespace-definition* behaves as if it were replaced by
|
| 176 |
|
| 177 |
``` bnf
|
| 178 |
+
'inline'ₒₚₜ 'namespace' 'unique ' '{ /* empty body */ }'
|
| 179 |
+
'using namespace' 'unique ' ';'
|
| 180 |
+
'namespace' 'unique ' '{' namespace-body '}'
|
| 181 |
```
|
| 182 |
|
| 183 |
where `inline` appears if and only if it appears in the
|
| 184 |
+
*unnamed-namespace-definition* and all occurrences of `unique ` in a
|
| 185 |
translation unit are replaced by the same identifier, and this
|
| 186 |
+
identifier differs from all other identifiers in the translation unit.
|
| 187 |
+
The optional *attribute-specifier-seq* in the
|
| 188 |
+
*unnamed-namespace-definition* appertains to `unique `.
|
| 189 |
+
|
| 190 |
+
[*Example 1*:
|
| 191 |
|
| 192 |
``` cpp
|
| 193 |
namespace { int i; } // unique ::i
|
| 194 |
void f() { i++; } // unique ::i++
|
| 195 |
|
|
|
|
| 207 |
A::i++; // A::unique ::i
|
| 208 |
j++; // A::unique ::j
|
| 209 |
}
|
| 210 |
```
|
| 211 |
|
| 212 |
+
— *end example*]
|
| 213 |
+
|
| 214 |
#### Namespace member definitions <a id="namespace.memdef">[[namespace.memdef]]</a>
|
| 215 |
|
| 216 |
+
A declaration in a namespace `N` (excluding declarations in nested
|
| 217 |
+
scopes) whose *declarator-id* is an *unqualified-id* ([[dcl.meaning]]),
|
| 218 |
+
whose *class-head-name* (Clause [[class]]) or *enum-head-name* (
|
| 219 |
+
[[dcl.enum]]) is an *identifier*, or whose *elaborated-type-specifier*
|
| 220 |
+
is of the form *class-key* *attribute-specifier-seq*ₒₚₜ *identifier* (
|
| 221 |
+
[[dcl.type.elab]]), or that is an *opaque-enum-declaration*, declares
|
| 222 |
+
(or redeclares) its *unqualified-id* or *identifier* as a member of `N`.
|
| 223 |
+
|
| 224 |
+
[*Note 1*: An explicit instantiation ([[temp.explicit]]) or explicit
|
| 225 |
+
specialization ([[temp.expl.spec]]) of a template does not introduce a
|
| 226 |
+
name and thus may be declared using an *unqualified-id* in a member of
|
| 227 |
+
the enclosing namespace set, if the primary template is declared in an
|
| 228 |
+
inline namespace. — *end note*]
|
| 229 |
+
|
| 230 |
+
[*Example 1*:
|
| 231 |
|
| 232 |
``` cpp
|
| 233 |
namespace X {
|
| 234 |
+
void f() { ... } // OK: introduces X::f()
|
| 235 |
+
|
| 236 |
+
namespace M {
|
| 237 |
+
void g(); // OK: introduces X::M::g()
|
| 238 |
+
}
|
| 239 |
+
using M::g;
|
| 240 |
+
void g(); // error: conflicts with X::M::g()
|
| 241 |
}
|
| 242 |
```
|
| 243 |
|
| 244 |
+
— *end example*]
|
| 245 |
+
|
| 246 |
Members of a named namespace can also be defined outside that namespace
|
| 247 |
by explicit qualification ([[namespace.qual]]) of the name being
|
| 248 |
defined, provided that the entity being defined was already declared in
|
| 249 |
the namespace and the definition appears after the point of declaration
|
| 250 |
in a namespace that encloses the declaration’s namespace.
|
| 251 |
|
| 252 |
+
[*Example 2*:
|
| 253 |
+
|
| 254 |
``` cpp
|
| 255 |
namespace Q {
|
| 256 |
namespace V {
|
| 257 |
void f();
|
| 258 |
}
|
| 259 |
+
void V::f() { ... } // OK
|
| 260 |
+
void V::g() { ... } // error: g() is not yet a member of V
|
| 261 |
namespace V {
|
| 262 |
void g();
|
| 263 |
}
|
| 264 |
}
|
| 265 |
|
| 266 |
namespace R {
|
| 267 |
+
void Q::V::g() { ... } // error: R doesn't enclose Q
|
| 268 |
}
|
| 269 |
```
|
| 270 |
|
| 271 |
+
— *end example*]
|
| 272 |
+
|
| 273 |
If a `friend` declaration in a non-local class first declares a class,
|
| 274 |
+
function, class template or function template[^5] the friend is a member
|
| 275 |
of the innermost enclosing namespace. The `friend` declaration does not
|
| 276 |
by itself make the name visible to unqualified lookup (
|
| 277 |
[[basic.lookup.unqual]]) or qualified lookup ([[basic.lookup.qual]]).
|
| 278 |
+
|
| 279 |
+
[*Note 2*: The name of the friend will be visible in its namespace if a
|
| 280 |
+
matching declaration is provided at namespace scope (either before or
|
| 281 |
+
after the class definition granting friendship). — *end note*]
|
| 282 |
+
|
| 283 |
+
If a friend function or function template is called, its name may be
|
| 284 |
+
found by the name lookup that considers functions from namespaces and
|
| 285 |
+
classes associated with the types of the function arguments (
|
| 286 |
+
[[basic.lookup.argdep]]). If the name in a `friend` declaration is
|
| 287 |
+
neither qualified nor a *template-id* and the declaration is a function
|
| 288 |
+
or an *elaborated-type-specifier*, the lookup to determine whether the
|
| 289 |
+
entity has been previously declared shall not consider any scopes
|
| 290 |
+
outside the innermost enclosing namespace.
|
| 291 |
+
|
| 292 |
+
[*Note 3*: The other forms of `friend` declarations cannot declare a
|
| 293 |
+
new member of the innermost enclosing namespace and thus follow the
|
| 294 |
+
usual lookup rules. — *end note*]
|
| 295 |
+
|
| 296 |
+
[*Example 3*:
|
| 297 |
|
| 298 |
``` cpp
|
| 299 |
// Assume f and g have not yet been declared.
|
| 300 |
void h(int);
|
| 301 |
template <class T> void f2(T);
|
|
|
|
| 311 |
};
|
| 312 |
|
| 313 |
// A::f, A::g and A::h are not visible here
|
| 314 |
X x;
|
| 315 |
void g() { f(x); } // definition of A::g
|
| 316 |
+
void f(X) { ... } // definition of A::f
|
| 317 |
+
void h(int) { ... } // definition of A::h
|
| 318 |
// A::f, A::g and A::h are visible here and known to be friends
|
| 319 |
}
|
| 320 |
|
| 321 |
using A::x;
|
| 322 |
|
|
|
|
| 325 |
A::X::f(x); // error: f is not a member of A::X
|
| 326 |
A::X::Y::g(); // error: g is not a member of A::X::Y
|
| 327 |
}
|
| 328 |
```
|
| 329 |
|
| 330 |
+
— *end example*]
|
| 331 |
+
|
| 332 |
### Namespace alias <a id="namespace.alias">[[namespace.alias]]</a>
|
| 333 |
|
| 334 |
A *namespace-alias-definition* declares an alternate name for a
|
| 335 |
namespace according to the following grammar:
|
| 336 |
|
|
|
|
| 349 |
nested-name-specifierₒₚₜ namespace-name
|
| 350 |
```
|
| 351 |
|
| 352 |
The *identifier* in a *namespace-alias-definition* is a synonym for the
|
| 353 |
name of the namespace denoted by the *qualified-namespace-specifier* and
|
| 354 |
+
becomes a *namespace-alias*.
|
| 355 |
+
|
| 356 |
+
[*Note 1*: When looking up a *namespace-name* in a
|
| 357 |
*namespace-alias-definition*, only namespace names are considered, see
|
| 358 |
+
[[basic.lookup.udir]]. — *end note*]
|
| 359 |
|
| 360 |
In a declarative region, a *namespace-alias-definition* can be used to
|
| 361 |
redefine a *namespace-alias* declared in that declarative region to
|
| 362 |
+
refer only to the namespace to which it already refers.
|
| 363 |
+
|
| 364 |
+
[*Example 1*:
|
| 365 |
+
|
| 366 |
+
The following declarations are well-formed:
|
| 367 |
|
| 368 |
``` cpp
|
| 369 |
+
namespace Company_with_very_long_name { ... }
|
| 370 |
namespace CWVLN = Company_with_very_long_name;
|
| 371 |
namespace CWVLN = Company_with_very_long_name; // OK: duplicate
|
| 372 |
namespace CWVLN = CWVLN;
|
| 373 |
```
|
| 374 |
|
| 375 |
+
— *end example*]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 376 |
|
| 377 |
### The `using` declaration <a id="namespace.udecl">[[namespace.udecl]]</a>
|
| 378 |
|
|
|
|
|
|
|
|
|
|
| 379 |
``` bnf
|
| 380 |
using-declaration:
|
| 381 |
+
'using' using-declarator-list ';'
|
|
|
|
| 382 |
```
|
| 383 |
|
| 384 |
+
``` bnf
|
| 385 |
+
using-declarator-list:
|
| 386 |
+
using-declarator '...'ₒₚₜ
|
| 387 |
+
using-declarator-list ',' using-declarator '...'ₒₚₜ
|
| 388 |
+
```
|
| 389 |
+
|
| 390 |
+
``` bnf
|
| 391 |
+
using-declarator:
|
| 392 |
+
'typename'ₒₚₜ nested-name-specifier unqualified-id
|
| 393 |
+
```
|
| 394 |
+
|
| 395 |
+
Each *using-declarator* in a *using-declaration* [^6] introduces a set
|
| 396 |
+
of declarations into the declarative region in which the
|
| 397 |
+
*using-declaration* appears. The set of declarations introduced by the
|
| 398 |
+
*using-declarator* is found by performing qualified name lookup (
|
| 399 |
+
[[basic.lookup.qual]], [[class.member.lookup]]) for the name in the
|
| 400 |
+
*using-declarator*, excluding functions that are hidden as described
|
| 401 |
+
below. If the *using-declarator* does not name a constructor, the
|
| 402 |
+
*unqualified-id* is declared in the declarative region in which the
|
| 403 |
+
*using-declaration* appears as a synonym for each declaration introduced
|
| 404 |
+
by the *using-declarator*.
|
| 405 |
+
|
| 406 |
+
[*Note 1*: Only the specified name is so declared; specifying an
|
| 407 |
+
enumeration name in a *using-declaration* does not declare its
|
| 408 |
+
enumerators in the *using-declaration*'s declarative
|
| 409 |
+
region. — *end note*]
|
| 410 |
+
|
| 411 |
+
If the *using-declarator* names a constructor, it declares that the
|
| 412 |
+
class *inherits* the set of constructor declarations introduced by the
|
| 413 |
+
*using-declarator* from the nominated base class.
|
| 414 |
|
| 415 |
Every *using-declaration* is a *declaration* and a *member-declaration*
|
| 416 |
+
and can therefore be used in a class definition.
|
| 417 |
+
|
| 418 |
+
[*Example 1*:
|
| 419 |
|
| 420 |
``` cpp
|
| 421 |
struct B {
|
| 422 |
void f(char);
|
| 423 |
void g(char);
|
|
|
|
| 430 |
void f(int) { f('c'); } // calls B::f(char)
|
| 431 |
void g(int) { g('c'); } // recursively calls D::g(int)
|
| 432 |
};
|
| 433 |
```
|
| 434 |
|
| 435 |
+
— *end example*]
|
| 436 |
+
|
| 437 |
+
In a *using-declaration* used as a *member-declaration*, each
|
| 438 |
+
*using-declarator*'s *nested-name-specifier* shall name a base class of
|
| 439 |
+
the class being defined. If a *using-declarator* names a constructor,
|
| 440 |
+
its *nested-name-specifier* shall name a direct base class of the class
|
| 441 |
+
being defined.
|
| 442 |
+
|
| 443 |
+
[*Example 2*:
|
| 444 |
+
|
| 445 |
+
``` cpp
|
| 446 |
+
template <typename... bases>
|
| 447 |
+
struct X : bases... {
|
| 448 |
+
using bases::g...;
|
| 449 |
+
};
|
| 450 |
+
|
| 451 |
+
X<B, D> x; // OK: B::g and D::g introduced
|
| 452 |
+
```
|
| 453 |
+
|
| 454 |
+
— *end example*]
|
| 455 |
+
|
| 456 |
+
[*Example 3*:
|
| 457 |
|
| 458 |
``` cpp
|
| 459 |
class C {
|
| 460 |
int g();
|
| 461 |
};
|
|
|
|
| 466 |
using B::x; // OK: x is a union member of base B
|
| 467 |
using C::g; // error: C isn't a base of D2
|
| 468 |
};
|
| 469 |
```
|
| 470 |
|
| 471 |
+
— *end example*]
|
| 472 |
+
|
| 473 |
+
[*Note 2*: Since destructors do not have names, a *using-declaration*
|
| 474 |
+
cannot refer to a destructor for a base class. Since specializations of
|
| 475 |
+
member templates for conversion functions are not found by name lookup,
|
| 476 |
+
they are not considered when a *using-declaration* specifies a
|
| 477 |
+
conversion function ([[temp.mem]]). — *end note*]
|
| 478 |
+
|
| 479 |
+
If a constructor or assignment operator brought from a base class into a
|
| 480 |
+
derived class has the signature of a copy/move constructor or assignment
|
| 481 |
+
operator for the derived class ([[class.copy]]), the
|
| 482 |
*using-declaration* does not by itself suppress the implicit declaration
|
| 483 |
+
of the derived class member; the member from the base class is hidden or
|
| 484 |
+
overridden by the implicitly-declared copy/move constructor or
|
| 485 |
+
assignment operator of the derived class, as described below.
|
|
|
|
| 486 |
|
| 487 |
A *using-declaration* shall not name a *template-id*.
|
| 488 |
|
| 489 |
+
[*Example 4*:
|
| 490 |
+
|
| 491 |
``` cpp
|
| 492 |
struct A {
|
| 493 |
template <class T> void f(T);
|
| 494 |
template <class T> struct X { };
|
| 495 |
};
|
|
|
|
| 497 |
using A::f<double>; // ill-formed
|
| 498 |
using A::X<int>; // ill-formed
|
| 499 |
};
|
| 500 |
```
|
| 501 |
|
| 502 |
+
— *end example*]
|
| 503 |
+
|
| 504 |
A *using-declaration* shall not name a namespace.
|
| 505 |
|
| 506 |
A *using-declaration* shall not name a scoped enumerator.
|
| 507 |
|
| 508 |
+
A *using-declaration* that names a class member shall be a
|
| 509 |
*member-declaration*.
|
| 510 |
|
| 511 |
+
[*Example 5*:
|
| 512 |
+
|
| 513 |
``` cpp
|
| 514 |
struct X {
|
| 515 |
int i;
|
| 516 |
static int s;
|
| 517 |
};
|
| 518 |
|
| 519 |
void f() {
|
| 520 |
+
using X::i; // error: X::i is a class member and this is not a member declaration.
|
| 521 |
+
using X::s; // error: X::s is a class member and this is not a member declaration.
|
|
|
|
|
|
|
| 522 |
}
|
| 523 |
```
|
| 524 |
|
| 525 |
+
— *end example*]
|
| 526 |
+
|
| 527 |
Members declared by a *using-declaration* can be referred to by explicit
|
| 528 |
+
qualification just like other member names ([[namespace.qual]]).
|
| 529 |
+
|
| 530 |
+
[*Example 6*:
|
| 531 |
|
| 532 |
``` cpp
|
| 533 |
void f();
|
| 534 |
|
| 535 |
namespace A {
|
|
|
|
| 546 |
X::f(); // calls ::f
|
| 547 |
X::g(); // calls A::g
|
| 548 |
}
|
| 549 |
```
|
| 550 |
|
| 551 |
+
— *end example*]
|
| 552 |
+
|
| 553 |
A *using-declaration* is a *declaration* and can therefore be used
|
| 554 |
repeatedly where (and only where) multiple declarations are allowed.
|
| 555 |
|
| 556 |
+
[*Example 7*:
|
| 557 |
+
|
| 558 |
``` cpp
|
| 559 |
namespace A {
|
| 560 |
int i;
|
| 561 |
}
|
| 562 |
|
| 563 |
namespace A1 {
|
| 564 |
+
using A::i, A::i; // OK: double declaration
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 565 |
}
|
| 566 |
|
| 567 |
struct B {
|
| 568 |
int i;
|
| 569 |
};
|
| 570 |
|
| 571 |
struct X : B {
|
| 572 |
+
using B::i, B::i; // error: double member declaration
|
|
|
|
| 573 |
};
|
| 574 |
```
|
| 575 |
|
| 576 |
+
— *end example*]
|
| 577 |
+
|
| 578 |
+
[*Note 3*: For a *using-declaration* whose *nested-name-specifier*
|
| 579 |
+
names a namespace, members added to the namespace after the
|
| 580 |
+
*using-declaration* are not in the set of introduced declarations, so
|
| 581 |
+
they are not considered when a use of the name is made. Thus, additional
|
| 582 |
+
overloads added after the *using-declaration* are ignored, but default
|
| 583 |
+
function arguments ([[dcl.fct.default]]), default template arguments (
|
| 584 |
[[temp.param]]), and template specializations ([[temp.class.spec]],
|
| 585 |
+
[[temp.expl.spec]]) are considered. — *end note*]
|
| 586 |
+
|
| 587 |
+
[*Example 8*:
|
| 588 |
|
| 589 |
``` cpp
|
| 590 |
namespace A {
|
| 591 |
void f(int);
|
| 592 |
}
|
| 593 |
|
| 594 |
+
using A::f; // f is a synonym for A::f; that is, for A::f(int).
|
|
|
|
| 595 |
namespace A {
|
| 596 |
void f(char);
|
| 597 |
}
|
| 598 |
|
| 599 |
void foo() {
|
| 600 |
+
f('a'); // calls f(int), even though f(char) exists.
|
| 601 |
+
}
|
| 602 |
|
| 603 |
void bar() {
|
| 604 |
+
using A::f; // f is a synonym for A::f; that is, for A::f(int) and A::f(char).
|
|
|
|
| 605 |
f('a'); // calls f(char)
|
| 606 |
}
|
| 607 |
```
|
| 608 |
|
| 609 |
+
— *end example*]
|
| 610 |
+
|
| 611 |
+
[*Note 4*: Partial specializations of class templates are found by
|
| 612 |
+
looking up the primary class template and then considering all partial
|
| 613 |
+
specializations of that template. If a *using-declaration* names a class
|
| 614 |
+
template, partial specializations introduced after the
|
| 615 |
+
*using-declaration* are effectively visible because the primary template
|
| 616 |
+
is visible ([[temp.class.spec]]). — *end note*]
|
| 617 |
|
| 618 |
Since a *using-declaration* is a declaration, the restrictions on
|
| 619 |
declarations of the same name in the same declarative region (
|
| 620 |
[[basic.scope]]) also apply to *using-declaration*s.
|
| 621 |
|
| 622 |
+
[*Example 9*:
|
| 623 |
+
|
| 624 |
``` cpp
|
| 625 |
namespace A {
|
| 626 |
int x;
|
| 627 |
}
|
| 628 |
|
|
|
|
| 649 |
x = 99; // assigns to A::x
|
| 650 |
struct x x1; // x1 has class type B::x
|
| 651 |
}
|
| 652 |
```
|
| 653 |
|
| 654 |
+
— *end example*]
|
| 655 |
+
|
| 656 |
If a function declaration in namespace scope or block scope has the same
|
| 657 |
name and the same parameter-type-list ([[dcl.fct]]) as a function
|
| 658 |
introduced by a *using-declaration*, and the declarations do not declare
|
| 659 |
the same function, the program is ill-formed. If a function template
|
| 660 |
declaration in namespace scope has the same name, parameter-type-list,
|
| 661 |
return type, and template parameter list as a function template
|
| 662 |
+
introduced by a *using-declaration*, the program is ill-formed.
|
| 663 |
+
|
| 664 |
+
[*Note 5*:
|
| 665 |
+
|
| 666 |
+
Two *using-declaration*s may introduce functions with the same name and
|
| 667 |
+
the same parameter-type-list. If, for a call to an unqualified function
|
| 668 |
name, function overload resolution selects the functions introduced by
|
| 669 |
such *using-declaration*s, the function call is ill-formed.
|
| 670 |
|
| 671 |
+
[*Example 10*:
|
| 672 |
+
|
| 673 |
``` cpp
|
| 674 |
namespace B {
|
| 675 |
void f(int);
|
| 676 |
void f(double);
|
| 677 |
}
|
|
|
|
| 688 |
f(1); // error: ambiguous: B::f(int) or C::f(int)?
|
| 689 |
void f(int); // error: f(int) conflicts with C::f(int) and B::f(int)
|
| 690 |
}
|
| 691 |
```
|
| 692 |
|
| 693 |
+
— *end example*]
|
| 694 |
+
|
| 695 |
+
— *end note*]
|
| 696 |
+
|
| 697 |
+
When a *using-declarator* brings declarations from a base class into a
|
| 698 |
+
derived class, member functions and member function templates in the
|
| 699 |
derived class override and/or hide member functions and member function
|
| 700 |
templates with the same name, parameter-type-list ([[dcl.fct]]),
|
| 701 |
cv-qualification, and *ref-qualifier* (if any) in a base class (rather
|
| 702 |
+
than conflicting). Such hidden or overridden declarations are excluded
|
| 703 |
+
from the set of declarations introduced by the *using-declarator*.
|
| 704 |
+
|
| 705 |
+
[*Example 11*:
|
| 706 |
|
| 707 |
``` cpp
|
| 708 |
struct B {
|
| 709 |
virtual void f(int);
|
| 710 |
virtual void f(char);
|
|
|
|
| 728 |
p->f(1); // calls D::f(int)
|
| 729 |
p->f('a'); // calls B::f(char)
|
| 730 |
p->g(1); // calls B::g(int)
|
| 731 |
p->g('a'); // calls D::g(char)
|
| 732 |
}
|
| 733 |
+
|
| 734 |
+
struct B1 {
|
| 735 |
+
B1(int);
|
| 736 |
+
};
|
| 737 |
+
|
| 738 |
+
struct B2 {
|
| 739 |
+
B2(int);
|
| 740 |
+
};
|
| 741 |
+
|
| 742 |
+
struct D1 : B1, B2 {
|
| 743 |
+
using B1::B1;
|
| 744 |
+
using B2::B2;
|
| 745 |
+
};
|
| 746 |
+
D1 d1(0); // ill-formed: ambiguous
|
| 747 |
+
|
| 748 |
+
struct D2 : B1, B2 {
|
| 749 |
+
using B1::B1;
|
| 750 |
+
using B2::B2;
|
| 751 |
+
D2(int); // OK: D2::D2(int) hides B1::B1(int) and B2::B2(int)
|
| 752 |
+
};
|
| 753 |
+
D2 d2(0); // calls D2::D2(int)
|
| 754 |
```
|
| 755 |
|
| 756 |
+
— *end example*]
|
| 757 |
+
|
| 758 |
+
For the purpose of overload resolution, the functions that are
|
| 759 |
+
introduced by a *using-declaration* into a derived class are treated as
|
| 760 |
+
though they were members of the derived class. In particular, the
|
| 761 |
implicit `this` parameter shall be treated as if it were a pointer to
|
| 762 |
the derived class rather than to the base class. This has no effect on
|
| 763 |
the type of the function, and in all other respects the function remains
|
| 764 |
+
a member of the base class. Likewise, constructors that are introduced
|
| 765 |
+
by a *using-declaration* are treated as though they were constructors of
|
| 766 |
+
the derived class when looking up the constructors of the derived
|
| 767 |
+
class ([[class.qual]]) or forming a set of overload candidates (
|
| 768 |
+
[[over.match.ctor]], [[over.match.copy]], [[over.match.list]]). If such
|
| 769 |
+
a constructor is selected to perform the initialization of an object of
|
| 770 |
+
class type, all subobjects other than the base class from which the
|
| 771 |
+
constructor originated are implicitly initialized (
|
| 772 |
+
[[class.inhctor.init]]).
|
| 773 |
+
|
| 774 |
+
In a *using-declarator* that does not name a constructor, all members of
|
| 775 |
+
the set of introduced declarations shall be accessible. In a
|
| 776 |
+
*using-declarator* that names a constructor, no access check is
|
| 777 |
+
performed. In particular, if a derived class uses a *using-declarator*
|
| 778 |
+
to access a member of a base class, the member name shall be accessible.
|
| 779 |
+
If the name is that of an overloaded member function, then all functions
|
| 780 |
+
named shall be accessible. The base class members mentioned by a
|
| 781 |
+
*using-declarator* shall be visible in the scope of at least one of the
|
| 782 |
+
direct base classes of the class where the *using-declarator* is
|
| 783 |
+
specified.
|
| 784 |
+
|
| 785 |
+
[*Note 6*:
|
| 786 |
+
|
| 787 |
+
Because a *using-declarator* designates a base class member (and not a
|
| 788 |
+
member subobject or a member function of a base class subobject), a
|
| 789 |
+
*using-declarator* cannot be used to resolve inherited member
|
| 790 |
+
ambiguities.
|
| 791 |
+
|
| 792 |
+
[*Example 12*:
|
| 793 |
|
| 794 |
``` cpp
|
| 795 |
struct A { int x(); };
|
| 796 |
struct B : A { };
|
| 797 |
struct C : A {
|
|
|
|
| 802 |
struct D : B, C {
|
| 803 |
using C::x;
|
| 804 |
int x(double);
|
| 805 |
};
|
| 806 |
int f(D* d) {
|
| 807 |
+
return d->x(); // error: overload resolution selects A::x, but A is an ambiguous base class
|
| 808 |
}
|
| 809 |
```
|
| 810 |
|
| 811 |
+
— *end example*]
|
| 812 |
+
|
| 813 |
+
— *end note*]
|
| 814 |
+
|
| 815 |
+
A synonym created by a *using-declaration* has the usual accessibility
|
| 816 |
+
for a *member-declaration*. A *using-declarator* that names a
|
| 817 |
+
constructor does not create a synonym; instead, the additional
|
| 818 |
+
constructors are accessible if they would be accessible when used to
|
| 819 |
+
construct an object of the corresponding base class, and the
|
| 820 |
+
accessibility of the *using-declaration* is ignored.
|
| 821 |
+
|
| 822 |
+
[*Example 13*:
|
| 823 |
|
| 824 |
``` cpp
|
| 825 |
class A {
|
| 826 |
private:
|
| 827 |
void f(char);
|
|
|
|
| 836 |
public:
|
| 837 |
using A::g; // B::g is a public synonym for A::g
|
| 838 |
};
|
| 839 |
```
|
| 840 |
|
| 841 |
+
— *end example*]
|
| 842 |
+
|
| 843 |
+
If a *using-declarator* uses the keyword `typename` and specifies a
|
| 844 |
dependent name ([[temp.dep]]), the name introduced by the
|
| 845 |
*using-declaration* is treated as a *typedef-name* ([[dcl.typedef]]).
|
| 846 |
|
| 847 |
### Using directive <a id="namespace.udir">[[namespace.udir]]</a>
|
| 848 |
|
|
|
|
| 850 |
using-directive:
|
| 851 |
attribute-specifier-seqₒₚₜ 'using namespace' nested-name-specifierₒₚₜ namespace-name ';'
|
| 852 |
```
|
| 853 |
|
| 854 |
A *using-directive* shall not appear in class scope, but may appear in
|
| 855 |
+
namespace scope or in block scope.
|
| 856 |
+
|
| 857 |
+
[*Note 1*: When looking up a *namespace-name* in a *using-directive*,
|
| 858 |
+
only namespace names are considered, see
|
| 859 |
+
[[basic.lookup.udir]]. — *end note*]
|
| 860 |
+
|
| 861 |
+
The optional *attribute-specifier-seq* appertains to the
|
| 862 |
+
*using-directive*.
|
| 863 |
|
| 864 |
A *using-directive* specifies that the names in the nominated namespace
|
| 865 |
can be used in the scope in which the *using-directive* appears after
|
| 866 |
the *using-directive*. During unqualified name lookup (
|
| 867 |
[[basic.lookup.unqual]]), the names appear as if they were declared in
|
| 868 |
the nearest enclosing namespace which contains both the
|
| 869 |
+
*using-directive* and the nominated namespace.
|
| 870 |
+
|
| 871 |
+
[*Note 2*: In this context, “contains” means “contains directly or
|
| 872 |
+
indirectly”. — *end note*]
|
| 873 |
|
| 874 |
A *using-directive* does not add any members to the declarative region
|
| 875 |
in which it appears.
|
| 876 |
|
| 877 |
+
[*Example 1*:
|
| 878 |
+
|
| 879 |
``` cpp
|
| 880 |
namespace A {
|
| 881 |
int i;
|
| 882 |
namespace B {
|
| 883 |
namespace C {
|
|
|
|
| 902 |
void f4() {
|
| 903 |
i = 5; // ill-formed; neither i is visible
|
| 904 |
}
|
| 905 |
```
|
| 906 |
|
| 907 |
+
— *end example*]
|
| 908 |
+
|
| 909 |
For unqualified lookup ([[basic.lookup.unqual]]), the *using-directive*
|
| 910 |
is transitive: if a scope contains a *using-directive* that nominates a
|
| 911 |
second namespace that itself contains *using-directive*s, the effect is
|
| 912 |
as if the *using-directive*s from the second namespace also appeared in
|
| 913 |
+
the first.
|
| 914 |
+
|
| 915 |
+
[*Note 3*: For qualified lookup, see
|
| 916 |
+
[[namespace.qual]]. — *end note*]
|
| 917 |
+
|
| 918 |
+
[*Example 2*:
|
| 919 |
|
| 920 |
``` cpp
|
| 921 |
namespace M {
|
| 922 |
int i;
|
| 923 |
}
|
|
|
|
| 956 |
int n = j; // D::j hides B::j
|
| 957 |
}
|
| 958 |
}
|
| 959 |
```
|
| 960 |
|
| 961 |
+
— *end example*]
|
| 962 |
+
|
| 963 |
+
If a namespace is extended ([[namespace.def]]) after a
|
| 964 |
+
*using-directive* for that namespace is given, the additional members of
|
| 965 |
+
the extended namespace and the members of namespaces nominated by
|
| 966 |
+
*using-directive*s in the extending *namespace-definition* can be used
|
| 967 |
+
after the extending *namespace-definition*.
|
| 968 |
|
| 969 |
If name lookup finds a declaration for a name in two different
|
| 970 |
namespaces, and the declarations do not declare the same entity and do
|
| 971 |
+
not declare functions, the use of the name is ill-formed.
|
| 972 |
+
|
| 973 |
+
[*Note 4*:
|
| 974 |
+
|
| 975 |
+
In particular, the name of a variable, function or enumerator does not
|
| 976 |
+
hide the name of a class or enumeration declared in a different
|
| 977 |
+
namespace. For example,
|
| 978 |
|
| 979 |
``` cpp
|
| 980 |
namespace A {
|
| 981 |
class X { };
|
| 982 |
extern "C" int g();
|
|
|
|
| 990 |
using namespace A;
|
| 991 |
using namespace B;
|
| 992 |
|
| 993 |
void f() {
|
| 994 |
X(1); // error: name X found in two namespaces
|
| 995 |
+
g(); // OK: name g refers to the same entity
|
| 996 |
+
h(); // OK: overload resolution selects A::h
|
| 997 |
}
|
| 998 |
```
|
| 999 |
|
| 1000 |
+
— *end note*]
|
| 1001 |
+
|
| 1002 |
During overload resolution, all functions from the transitive search are
|
| 1003 |
considered for argument matching. The set of declarations found by the
|
| 1004 |
+
transitive search is unordered.
|
| 1005 |
+
|
| 1006 |
+
[*Note 5*: In particular, the order in which namespaces were considered
|
| 1007 |
+
and the relationships among the namespaces implied by the
|
| 1008 |
+
*using-directive*s do not cause preference to be given to any of the
|
| 1009 |
+
declarations found by the search. — *end note*]
|
| 1010 |
+
|
| 1011 |
+
An ambiguity exists if the best match finds two functions with the same
|
| 1012 |
+
signature, even if one is in a namespace reachable through
|
| 1013 |
+
*using-directive*s in the namespace of the other.[^7]
|
| 1014 |
+
|
| 1015 |
+
[*Example 3*:
|
| 1016 |
|
| 1017 |
``` cpp
|
| 1018 |
namespace D {
|
| 1019 |
int d1;
|
| 1020 |
void f(char);
|
|
|
|
| 1043 |
f(1); // error: ambiguous: D::f(int) or E::f(int)?
|
| 1044 |
f('a'); // OK: D::f(char)
|
| 1045 |
}
|
| 1046 |
```
|
| 1047 |
|
| 1048 |
+
— *end example*]
|
| 1049 |
+
|