{"id":98835,"date":"2018-05-25T07:00:00","date_gmt":"2018-05-25T21:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/?p=98835"},"modified":"2019-03-13T00:44:23","modified_gmt":"2019-03-13T07:44:23","slug":"20180525-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20180525-00\/?p=98835","title":{"rendered":"My namespace importing trick imported the same three namespaces into each top-level namespace, yet it worked?"},"content":{"rendered":"<p>A little while ago, <a HREF=\"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/20180516-00\/?p=98765\">I noted a technique formally known as namespace composition<\/a>. There was one section that appeared to confuse some people: <\/p>\n<pre>\nnamespace ABI\n{\n  using namespace Windows::System::Profile::SystemManufacturers;\n  using namespace Windows::UI::ViewManagement;\n  using namespace Windows::Security::Cryptography;\n}\n\nnamespace cx\n{\n  using namespace Windows::System::Profile::SystemManufacturers;\n  using namespace Windows::UI::ViewManagement;\n  using namespace Windows::Security::Cryptography;\n}\n\nnamespace winrt\n{\n  using namespace Windows::System::Profile::SystemManufacturers;\n  using namespace Windows::UI::ViewManagement;\n  using namespace Windows::Security::Cryptography;\n}\n<\/pre>\n<p>Was this a copy\/paste error? After all, the same three namespaces are being imported each time. <\/p>\n<p>Well, no, actually. The text is the same, but each one is interpreted differently. <\/p>\n<p>Let&#8217;s take a simpler example: <\/p>\n<pre>\nnamespace X { namespace W { void f(); }}\nnamespace Y { namespace W { void f(); }}\nnamespace W { void f(); }\n\nnamespace X\n{\n    using namespace W;\n    auto do_something = f;\n}\n\nnamespace Y\n{\n    using namespace W;\n    auto do_something = f;\n}\n\nnamespace Z\n{\n    using namespace W;\n    auto do_something = f;\n}\n<\/pre>\n<p>Each of the three namespaces contain a <code>using namespace W;<\/code>, but each one refers to a different namespace, which you can see by pasting the above into <a HREF=\"https:\/\/gcc.godbolt.org\/\">Compiler Explorer<\/a> and observing the definitions of <code>X::<code><\/code>do_something<\/code>, <code>Y::<code><\/code>do_something<\/code>, and <code>Z::<code><\/code>do_something<\/code>. <\/p>\n<p>The first <code>using namespace W;<\/code> takes place inside a <code>namespace X<\/code>, so the search begins relative to that namespace, and we find it at <code>::X::W<\/code>. <\/p>\n<p>Similarly, the second <code>using namespace W;<\/code> takes place inside a <code>namespace Y<\/code>, so the search begins relative to that namespace, and we find it at <code>::Y::W<\/code>. <\/p>\n<p>The third <code>using namespace W;<\/code> takes place inside a <code>namespace Z<\/code>, so the search begins relative to that namespace. There is no <code>::Z::W<\/code>, so we resume our search at the next outer namespace, which is the global namespace, and we find it as <code>::W<\/code>. <\/p>\n<p>Even though the three namespace imports are textually identical, they have different effects because they each occur in different contexts. <\/p>\n<p>I wrote it this way because it showed that I was &#8220;pulling in&#8221; the relative namespace declarations into the corresponding first-level namespace. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>They are different namespaces, spelled the same.<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-98835","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>They are different namespaces, spelled the same.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/98835","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=98835"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/98835\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=98835"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=98835"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=98835"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}