Home > OS >  C Define function of external namespace inside of nested namespace
C Define function of external namespace inside of nested namespace

Time:11-11

Consider the code

namespace A
{    
}

namespace B
{
    namespace ::A
    {
        void func();
    }
}

This is invalid C syntax - but I'm looking for something that works. I need to declare ::A::func(), but from within namespace B (I don't want B::A::func, but A::func).

(If you ask why? then it is because func() will eventually by declared by a macro that also declares some things in namespace B.)

Is is possible?

CodePudding user response:

No it's not possible to somehow reset yourself to the global namespace within the scope of another namespace, for the purpose of declarations.

It could be useful on occasions though and C does pride itself on being a general purpose language, although it would make source code confusing to read as having used namespace ::A you are no longer in namespace B despite the source code telling you otherwise! If you really want to see this then why not propose it to the standards committee?

CodePudding user response:

You can use the using declaration. For example

namespace A
{
    void func();    
}

namespace B
{
    using A::func;
}

Then for example in the global namespace you can write

void other_func()
{
    B::func();
}
  • Related