Dave Mateer Dave Mateer - 18 days ago 12
C++ Question

C++ namespace alias and forward declaration

I am using a C++ third party library that places all of its classes in a versioned namespace, let's call it

tplib_v44
. They also define a generic namespace alias:

namespace tplib = tplib_v44;


If a forward-declare a member of the library in my own .h file using the generic namespace...

namespace tplib { class SomeClassInTpLib; }


... I get compiler errors on the header in the third-party library (which is being included later in my .cpp implementation file):

error C2386: 'tplib' : a symbol with this name already exists in the current scope


If I use the version-specific namespace, then everything works fine, but then ... what's the point? What's the best way to deal with this?

[EDIT] FYI for future viewers, this was the ICU library. A solution (at least in my situation) is in the comments to the accepted answer.

Answer

It looks like there is an ugly workaround for this, but no good solution.

For ACE (with a decent explanation) and Xerces (with a snarky "this is how c++ works" comment), they define macros that you can use to do this "generically".

ACE_BEGIN_VERSIONED_NAMESPACE_DECL
class ACE_Reactor;
ACE_END_VERSIONED_NAMESPACE_DECL

XERCES_CPP_NAMESPACE_BEGIN
class DOMDocument;
class DOMElement;
XERCES_CPP_NAMESPACE_END

It looks like an unfortunate c++ artifact, try searching around in your tplib for these macros.

The standard treats namespaces and namespace aliases as different things. You're declaring tplib as a namespace, so when the compiler tries to assign an alias later, it cannot be both, so the compiler complains.

Comments