2 <clause number="16.3.1" title="Using alias directives">
3 <paragraph>A <non_terminal where="16.3.1">using-alias-directive</non_terminal> introduces an identifier that serves as an alias for a namespace or type within the immediately enclosing compilation unit or namespace body. <grammar_production><name><non_terminal where="16.3.1">using-alias-directive</non_terminal></name> : <rhs><keyword>using</keyword><non_terminal where="9.4.2">identifier</non_terminal><terminal>=</terminal><non_terminal where="10.8">namespace-or-type-name</non_terminal><terminal>;</terminal></rhs></grammar_production></paragraph>
4 <paragraph>Within member declarations in a compilation unit or namespace body that contains a <non_terminal where="16.3.1">using-alias-directive</non_terminal>, the identifier introduced by the <non_terminal where="16.3.1">using-alias-directive</non_terminal> can be used to reference the given namespace or type. </paragraph>
6 <example>[Example: For example: <code_example><![CDATA[
16 ]]></code_example></example>
19 <example>Above, within member declarations in the N3 namespace, A is an alias for N1.N2.A, and thus class N3.B derives from class N1.N2.A. The same effect can be obtained by creating an alias R for N1.N2 and then referencing R.A: <code_example><![CDATA[
25 ]]></code_example>end example]</example>
27 <paragraph>The identifier of a <non_terminal where="16.3.1">using-alias-directive</non_terminal> must be unique within the declaration space of the compilation unit or namespace that immediately contains the <non_terminal where="16.3.1">using-alias-directive</non_terminal>. <example>[Example: For example: <code_example><![CDATA[
34 using A = N1.N2.A; // Error, A already exists
36 ]]></code_example></example></paragraph>
37 <paragraph><example>Above, N3 already contains a member A, so it is a compile-time error for a <non_terminal where="16.3.1">using-alias-directive</non_terminal> to use that identifier. end example]</example> Likewise, it is a compile-time error for two or more <non_terminal where="16.3.1">using-alias-directive</non_terminal>s in the same compilation unit or namespace body to declare aliases by the same name. </paragraph>
38 <paragraph>A <non_terminal where="16.3.1">using-alias-directive</non_terminal> makes an alias available within a particular compilation unit or namespace body, but it does not contribute any new members to the underlying declaration space. In other words, a <non_terminal where="16.3.1">using-alias-directive</non_terminal> is not transitive, but, rather, affects only the compilation unit or namespace body in which it occurs. </paragraph>
40 <example>[Example: In the example <code_example><![CDATA[
47 class B: R.A {} // Error, R unknown
49 ]]></code_example>the scope of the <non_terminal where="16.3.1">using-alias-directive</non_terminal> that introduces R only extends to member declarations in the namespace body in which it is contained, so R is unknown in the second namespace declaration. However, placing the <non_terminal where="16.3.1">using-alias-directive</non_terminal> in the containing compilation unit causes the alias to become available within both namespace declarations: <code_example><![CDATA[
59 ]]></code_example>end example]</example>
61 <paragraph>Just like regular members, names introduced by <non_terminal where="16.3.1">using-alias-directive</non_terminal>s are hidden by similarly named members in nested scopes. <example>[Example: In the example <code_example><![CDATA[
66 class B: R.A {} // Error, R has no member A
68 ]]></code_example>the reference to R.A in the declaration of B causes a compile-time error because R refers to N3.R, not N1.N2. end example]</example> </paragraph>
69 <paragraph>The order in which <non_terminal where="16.3.1">using-alias-directive</non_terminal>s are written has no significance, and resolution of the <non_terminal where="10.8">namespace-or-type-name</non_terminal> referenced by a <non_terminal where="16.3.1">using-alias-directive</non_terminal> is not affected by the <non_terminal where="16.3.1">using-alias-directive</non_terminal> itself or by other <non_terminal where="16.3">using-directive</non_terminal>s in the immediately containing compilation unit or namespace body. In other words, the <non_terminal where="10.8">namespace-or-type-name</non_terminal> of a <non_terminal where="16.3.1">using-alias-directive</non_terminal> is resolved as if the immediately containing compilation unit or namespace body had no <non_terminal where="16.3">using-directive</non_terminal>s. <example>[Example: In the example <code_example><![CDATA[
74 using R2 = N1.N2; // OK
75 using R3 = R1.N2; // Error, R1 unknown
77 ]]></code_example>the last <non_terminal where="16.3.1">using-alias-directive</non_terminal> results in a compile-time error because it is not affected by the first <non_terminal where="16.3.1">using-alias-directive</non_terminal>. end example]</example> </paragraph>
78 <paragraph>A <non_terminal where="16.3.1">using-alias-directive</non_terminal> can create an alias for any namespace or type, including the namespace within which it appears and any namespace or type nested within that namespace. </paragraph>
79 <paragraph>Accessing a namespace or type through an alias yields exactly the same result as accessing that namespace or type through its declared name. <example>[Example: For example, given <code_example><![CDATA[
90 N1.N2.A a; // refers to N1.N2.A
91 R1.N2.A b; // refers to N1.N2.A
92 R2.A c; // refers to N1.N2.A
95 ]]></code_example>the names N1.N2.A, R1.N2.A, and R2.A are equivalent and all refer to the class whose fully qualified name is N1.N2.A. end example]</example> </paragraph>