Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to solve two conflicting namespaces with the same name

Tags:

c++

namespaces

I have in my own code an namespace core that is conflicting with the namespace core of a dynamic library that I am using. The problem appears because I can not modify neither one nor the other. The first core because is part of our team project, and the second because is part of the library. Is there a way to solve this situation?

like image 878
J. Velazquez-Muriel Avatar asked Jan 17 '23 03:01

J. Velazquez-Muriel


1 Answers

Try turning your core namespace into a sub namespace.

EDIT: Cleaned up answer and added code. (Hard to do on mobile!)

This way you can get access to each 'core' namespace. This also explicitly shows which 'core' is being referred to. Even if the external library has the same function names you will be safe.

However, you mentioned that you can't actually change anything in the original code, so your only option is to use a different library and hope it doesn't have a 'core' namespace in it. Seems to me like it might be alot easier to convince your team to change their code.

// Your 'core' namespace, inside your own namespace.
namespace Mygame
{
  namespace core
  {
    void Foo();
  }
}

// Access both namespaces safely.
Mygame::core::Foo();
core::Foo();
like image 108
Aesthete Avatar answered Feb 05 '23 21:02

Aesthete