17

Assume I have the method defined in the two different namespaces:

namespace foo
{
    void print()
    {
        //do work...
    }
}

namespace bar
{
    void print()
    {
        //do work...
    }
}

The foo::print() and the bar::print() functions are absolutely equal. My project uses the numerous calls of these functions.

Is there a way to remove one of the print() definitions without changing the calls of these function? I mean something like the following (of course, C++ language doesn't allow this construction, it's just an example):

namespace foo, bar  //wrong code!
{
    void print()
    {
        //do work...
    }
}

If there is no way to refactor the code as I want, please tell me, do you like the following decision? Will you be glad if your project contains such code? :)

namespace foo
{
    void print()
    {
        //do work...
    }
}

namespace bar
{
    void print()
    {
        foo::print();
    }
}

ADD:

Thank you guys, I'm fully satisfied by your answers. Just one moment I want you to clarify: is there a difference between using ::foo::print and using foo::print?

3
  • I can understand why you would unaccept my answer, but only if you meant to accept Haroogan's answer.
    – Agentlien
    Dec 30, 2013 at 10:46
  • Damn, I just clicked on the wrong space. Excuse me, please. I like both your answers but I'll choose Agentlien... just because his reputation is lower :) Anyway I don't know how to handle the situations then several users give the same right answer during the short time. So, Haroogan, I hope you are not upset by my decision
    – yakov
    Dec 30, 2013 at 10:52
  • No problem, it's always up to you. I pointed this out just because you've accidentally chosen the wrong answer with downvotes. So, now it's all good. Good luck. ;) Dec 30, 2013 at 16:26

5 Answers 5

18

You can achieve this with a using declaration.

namespace foo
{
    void print()
    {
        //do work...
    }
}

namespace bar
{
    using foo::print;
}

EDIT

Regarding the difference between ::foo::print and foo::print: prepending a qualified name with :: means that you explicitly refer to the one in the global namespace. This can be used to select the global one, even if there is another item with the same name closer in scope.

14

How about using declaration:

namespace foo
{
    void print()
    {
        // do work ...
    }
}

namespace bar
{
    using ::foo::print;
}

Using ::foo::print instead of foo::print is an important point. If you would have another foo inside of bar:

namespace foo
{
    void print()
    {
        // 1 ...
    }
}

namespace bar
{
    namespace foo
    {
        void print()
        {
            // 2 ...
        }
    }

    using   foo::print; // refers to 2
    using ::foo::print; // refers to 1
}

then you'd see the merit of using ::. To summarize, prepending :: is a safe way to ensure that another nested namespace foo, which could be potentially added in future, will not bring you any surprises.

1
  • 3
    The tip about :: is good, as long as we can assume foo actually is in global namespace.
    – Agentlien
    Dec 30, 2013 at 9:55
4

With a using declaration:

namespace bar
{
    using ::foo::print;
}
0

Probably the best solution is this:

namespace foo
{
    void print()
    {
        //do work...
    }
}

namespace bar
{
    void print()
    {
        foo::print();
    }
}

However you can write:

namespace foo
{
    void print()
    {
        //do work...
    }
}

namespace bar
{
    void print()
    {
        //do work...
    }
}

This is not recomended, but there is no evil. Compilers are smart enough to understand that these two functions are the same and can be used the same assembly code.

Remember, namespace keyword is for programmers, compiler doesn't care about them very much, for compiler namespaces are like prefix for functions/methods/classes.

-2

Sample

namespace Q {
    namespace V {
        void f(); // enclosing namespaces are the global namespace, Q, and Q::V
        class C { void m(); };
        }
        void V::f() { // enclosing namespaces are the global namespace, Q, and Q::V
        extern void h(); // ... so this declares Q::V::h
        }
        void V::C::m() { // enclosing namespaces are the global namespace, Q, and Q::V
    }
}
2
  • if it wasnt that obvious where this was copied from one could think this is an attempt of fraud. Jul 6, 2017 at 12:02
  • btw code only answers are bad, but stolen code only answers are really really bad Jul 6, 2017 at 12:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.