Search code examples
c++using-directives

Are using-declarations static?


I'm a bit confused about using-declarations. I understand that using foo::bar; imports the symbol bar from namespace foo into the current namespace, but does this happen statically or dynamically?

More specifically, do using-declarations lead to an overhead? Would it be possible to import different symbols with the same name depending on a condition? (that would be bad practice, but I'm curious all the same)

It feels like it should be static, but I can't find anything to confirm this...


Solution

  • Namespace resolution happens at compile time. You can not change them dynamically at run time.

    One way to import different symbols depending on condition is to use preprocessor directives and macros:

    #ifdef USEA
    using a::f;
    #else
    using b::f;
    #endif