Skip to content

Instantly share code, notes, and snippets.

@cculianu
Last active September 8, 2020 06:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cculianu/14765a93cdaf9a7c5729a192fd5b6b20 to your computer and use it in GitHub Desktop.
Save cculianu/14765a93cdaf9a7c5729a192fd5b6b20 to your computer and use it in GitHub Desktop.
Example for namespaces for discussion in cppreference.com wiki
namespace A {
constexpr int i = 2;
}
namespace B {
constexpr int i = 3;
int j;
namespace C {
namespace D {
using namespace A; // all names from A injected into D
int j;
int k;
int a = i; // i is B::i, because A::i is hidden by B::i
}
using namespace D; // names from D are injected into C
// names from A are also injected into C
int k = 89; // OK to declare name identical to one introduced by a using
//int l = k; // ambiguous: C::k or D::k
int m = i; // ok: B::i hides A::i
int n = j; // ok: D::j hides B::j
static_assert(C::i == 2); // this is ok, takes `i` from `A` via `C`
static_assert(i == 3); // this is ok (takes 'i' from B)
static_assert(::i == 2 || ::i == 3); // Compile failure; `i` not in global namespace
}
}
static_assert(i == 2 || i == 3); // Compile failure; `i` not in global namespace
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment