X hits on this document

830 views

0 shares

0 downloads

0 comments

204 / 396

Chapter ‎18   Unsafe code

namespace N3 { class A {} }

namespace N3 { using A = N1.N2.A;// Error, A already exists }

Above, N3 already contains a member A, so it is a compile-time error for a using-alias-directive to use that identifier. Likewise, it is a compile-time error for two or more using-alias-directives in the same compilation unit or namespace body to declare aliases by the same name.

A using-alias-directive 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 using-alias-directive is not transitive but rather affects only the compilation unit or namespace body in which it occurs. In the example

namespace N3 { using R = N1.N2; }

namespace N3 { class B: R.A {}// Error, R unknown }

the scope of the using-alias-directive 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 using-alias-directive in the containing compilation unit causes the alias to become available within both namespace declarations:

using R = N1.N2;

namespace N3 { class B: R.A {} }

namespace N3 { class C: R.A {} }

Just like regular members, names introduced by using-alias-directives are hidden by similarly named members in nested scopes. In the example

using R = N1.N2;

namespace N3 { class R {}

class B: R.A {}// Error, R has no member A }

the reference to R.A in the declaration of B causes a compile-time error because R refers to N3.R, not N1.N2.

Copyright Microsoft Corporation 1999-2003. All Rights Reserved.191

Document info
Document views830
Page views830
Page last viewedSun Dec 04 13:07:05 UTC 2016
Pages396
Paragraphs9401
Words133190

Comments