howtechstuffworks
howtechstuffworks

Reputation: 1926

What if there is a name clash even after using namespace?

We usually use namespaces to avoid name clashes in C++. But what if there are two different libraries which have a class, that has the same namespace name and class name? Is there a work around?

(PS: I am pretty sure, that its a rare scenario, I never faced it, just happened to cross my mind)

Upvotes: 3

Views: 1465

Answers (3)

ixe013
ixe013

Reputation: 10191

You can rename one of the namespace with

namespace <new> = <old>;

But that will only get you so far. Here is an example :

namespace aaa {
   int foo() { return 42; }
}

namespace zzz {
   int foo() { return -42; }
}

namespace bbb = aaa;

int main() {
    aaa::foo();         //Original namespace is still there
    bbb::foo();  //but it is also available with a new name
    zzz::foo();  //No clash, that's what namespaces are for
    return 0;
}

But since the rename is really just an alias, the aaa namespace stays. This code will get you a duplicate symbol error (no matter how deep it is buried in .h files) :

namespace aaa {
   int foo() { return 42; }
}

namespace aaa {
   int foo() { return -42; }
}

You can get around it somehow by wrapping the clashing namespace in your own namespace declaration, like this :

#include <stdio.h>

namespace aaa {
   int foo() { return 42; }
}

namespace zzz {
#include "clash.h"
}

namespace aaa2 = zzz::aaa;

int main() {
    printf("%d\n", aaa::foo());   //Original works
    printf("%d\n", aaa2::foo());  //Non-clashing through alias
    printf("%d\n", zzz::aaa::foo());  //Non-clashing real name
    return 0;
}

But that solution too will work only in trivial code, because all the included files in clash.h will end up in the zzz namespace.

+I think renaming std is forbidden though, if only by common sense.

Upvotes: 0

Komi Golov
Komi Golov

Reputation: 3471

It might happen, but its not very likely to do you much damage. Not only do the namespace names have to be exactly the same, you have to actually use both libraries. Chances are, if they've named their namespaces in some reasonable way, their names will indicate their purpose -- even if there are two libraries that use GenericXMLParser as their namespace, and probably have some class names in common, do you really expect to be using them both on one project?

This does make certain namespace names a bad idea, such as boost or Ogre. Anyone using those (except the original users) should expect their library to be unusable in many cases. A less common name, like Pie, is probably going to be fine for by far most people in by far most cases. If both libraries using Pie become widely used, one might have to change; in that case, the namespace renaming trick can be used to keep backwards compatibility with old code.

Upvotes: 0

Cratylus
Cratylus

Reputation: 54094

The idea is to use something related to the company/group itself.
This way the collision is avoided.
It is highly unlikely that a profesional library would choose something trivial like MyNamespace.

It would be like Company::Project::Module.

This is even clearer in java where you have org.apache etc

Upvotes: 1

Related Questions