Стандарт C++ 11 (1119564), страница 53
Текст из файла (страница 53)
[ Note: Two using-declarations may introduce functions with the same name andthe same parameter types. If, for a call to an unqualified function name, function overload resolution selectsthe functions introduced by such using-declarations, the function call is ill-formed. [ Example:§ 7.3.3168© ISO/IEC 2011 – All rights reservedISO/IEC 14882:2011(E)namespace B {void f(int);void f(double);}namespace C {void f(int);void f(double);void f(char);}void h() {using B::f;using C::f;f(’h’);f(1);void f(int);}//////////B::f(int) and B::f(double)C::f(int), C::f(double), and C::f(char)calls C::f(char)error: ambiguous: B::f(int) or C::f(int)?error: f(int) conflicts with C::f(int) and B::f(int)— end example ] — end note ]15When a using-declaration brings names from a base class into a derived class scope, member functions andmember function templates in the derived class override and/or hide member functions and member functiontemplates with the same name, parameter-type-list (8.3.5), cv-qualification, and ref-qualifier (if any) in abase class (rather than conflicting).
[ Note: For using-declarations that name a constructor, see 12.9. — endnote ] [ Example:struct B {virtual void f(int);virtual void f(char);void g(int);void h(int);};struct D : B {using B::f;void f(int);using B::g;void g(char);using B::h;void h(int);};void k(D* p){p->f(1);p->f(’a’);p->g(1);p->g(’a’);}// OK: D::f(int) overrides B::f(int);// OK// OK: D::h(int) hides B::h(int)////////callscallscallscallsD::f(int)B::f(char)B::g(int)D::g(char)— end example ]16For the purpose of overload resolution, the functions which are introduced by a using-declaration into aderived class will be treated as though they were members of the derived class. In particular, the implicit§ 7.3.3© ISO/IEC 2011 – All rights reserved169ISO/IEC 14882:2011(E)this parameter shall be treated as if it were a pointer to the derived class rather than to the base class.This has no effect on the type of the function, and in all other respects the function remains a member ofthe base class.17The access rules for inheriting constructors are specified in 12.9; otherwise all instances of the name mentionedin a using-declaration shall be accessible.
In particular, if a derived class uses a using-declaration to accessa member of a base class, the member name shall be accessible. If the name is that of an overloadedmember function, then all functions named shall be accessible. The base class members mentioned by ausing-declaration shall be visible in the scope of at least one of the direct base classes of the class where theusing-declaration is specified. [ Note: Because a using-declaration designates a base class member (and nota member subobject or a member function of a base class subobject), a using-declaration cannot be used toresolve inherited member ambiguities. For example,struct A { int x(); };struct B : A { };struct C : A {using A::x;int x(int);};struct D : B, C {using C::x;int x(double);};int f(D* d) {return d->x();}// ambiguous: B::x or C::x— end note ]18The alias created by the using-declaration has the usual accessibility for a member-declaration.
[ Note: Ausing-declaration that names a constructor does not create aliases; see 12.9 for the pertinent accessibilityrules. — end note ] [ Example:class A {private:void f(char);public:void f(int);protected:void g();};class B : public A {using A::f;// error: A::f(char) is inaccessiblepublic:using A::g;// B::g is a public synonym for A::g};— end example ]19If a using-declaration uses the keyword typename and specifies a dependent name (14.6.2), the name intro-§ 7.3.3170© ISO/IEC 2011 – All rights reservedISO/IEC 14882:2011(E)duced by the using-declaration is treated as a typedef-name (7.1.3).7.3.4Using directive[namespace.udir]using-directive:attribute-specifier-seqopt using namespace nested-name-specifieropt namespace-name ;1A using-directive shall not appear in class scope, but may appear in namespace scope or in block scope.[ Note: When looking up a namespace-name in a using-directive, only namespace names are considered,see 3.4.6.
— end note ] The optional attribute-specifier-seq appertains to the using-directive.2A using-directive specifies that the names in the nominated namespace can be used in the scope in which theusing-directive appears after the using-directive. During unqualified name lookup (3.4.1), the names appearas if they were declared in the nearest enclosing namespace which contains both the using-directive and thenominated namespace. [ Note: In this context, “contains” means “contains directly or indirectly”.
— endnote ]3A using-directive does not add any members to the declarative region in which it appears. [ Example:namespace A {int i;namespace B {namespace C {int i;}using namespacevoid f1() {i = 5;}}namespace D {using namespaceusing namespacevoid f2() {i = 5;}}void f3() {i = 5;}}void f4() {i = 5;}A::B::C;// OK, C::i visible in B and hides A::iB;C;// ambiguous, B::C::i or A::i?// uses A::i// ill-formed; neither i is visible— end example ]4For unqualified lookup (3.4.1), the using-directive is transitive: if a scope contains a using-directive thatnominates a second namespace that itself contains using-directives, the effect is as if the using-directivesfrom the second namespace also appeared in the first.
[ Note: For qualified lookup, see 3.4.3.2. — end note ][ Example:namespace M {int i;}namespace N {§ 7.3.4© ISO/IEC 2011 – All rights reserved171ISO/IEC 14882:2011(E)int i;using namespace M;}void f() {using namespace N;i = 7;// error: both M::i and N::i are visible}For another example,namespace A {int i;}namespace B {int i;int j;namespace C {namespace D {using namespace A;int j;int k;int a = i;// B::i hides A::i}using namespace D;int k = 89;// no problem yetint l = k;// ambiguous: C::k or D::kint m = i;// B::i hides A::iint n = j;// D::j hides B::j}}— end example ]5If a namespace is extended by an extension-namespace-definition after a using-directive for that namespaceis given, the additional members of the extended namespace and the members of namespaces nominated byusing-directives in the extension-namespace-definition can be used after the extension-namespace-definition.6If name lookup finds a declaration for a name in two different namespaces, and the declarations do notdeclare the same entity and do not declare functions, the use of the name is ill-formed.
[ Note: In particular,the name of a variable, function or enumerator does not hide the name of a class or enumeration declaredin a different namespace. For example,namespace A {class X { };extern "C"intextern "C++" int}namespace B {void X(int);extern "C"intextern "C++" int}using namespace A;using namespace B;g();h();g();h(int);§ 7.3.4172© ISO/IEC 2011 – All rights reservedISO/IEC 14882:2011(E)void f() {X(1);g();h();}// error: name X found in two namespaces// okay: name g refers to the same entity// okay: overload resolution selects A::h— end note ]7During overload resolution, all functions from the transitive search are considered for argument matching.The set of declarations found by the transitive search is unordered.
[ Note: In particular, the order in whichnamespaces were considered and the relationships among the namespaces implied by the using-directives donot cause preference to be given to any of the declarations found by the search. — end note ] An ambiguityexists if the best match finds two functions with the same signature, even if one is in a namespace reachablethrough using-directives in the namespace of the other.96 [ Example:namespace D {int d1;void f(char);}using namespace D;int d1;// OK: no conflict with D::d1namespace E {int e;void f(int);}namespace D {// namespace extensionint d2;using namespace E;void f(int);}void f() {d1++;::d1++;D::d1++;d2++;e++;f(1);f(’a’);}//////////////error: ambiguous ::d1 or D::d1?OKOKOK: D::d2OK: E::eerror: ambiguous: D::f(int) or E::f(int)?OK: D::f(char)— end example ]7.41The asm declaration[dcl.asm]An asm declaration has the formasm-definition:asm ( string-literal ) ;96) During name lookup in a class hierarchy, some ambiguities may be resolved by considering whether one member hidesthe other along some paths (10.2).
There is no such disambiguation when considering the set of names found as a result offollowing using-directives.§ 7.4© ISO/IEC 2011 – All rights reserved173ISO/IEC 14882:2011(E)The asm declaration is conditionally-supported; its meaning is implementation-defined. [ Note: Typically itis used to pass information through the implementation to an assembler. — end note ]7.5Linkage specifications[dcl.link]1All function types, function names with external linkage, and variable names with external linkage have alanguage linkage. [ Note: Some of the properties associated with an entity with language linkage are specificto each implementation and are not described here. For example, a particular language linkage may beassociated with a particular form of representing names of objects and functions with external linkage, orwith a particular calling convention, etc.