4

In my project I have to use a library whose symbols are not in a namespace. The library's classes have fairly short and generic names such as Logger and Reader. I also use some of these names in my project's own classes.

Consider the situation where my project's symbols are also not namespaced. The name conflict could be resolved by putting my code in a namespace and distinguishing by using qualified names (e.g. ::Logger and My::Logger), but I would prefer the library's symbols to be namespaced as well. Is there any way that I can "force" the library's symbols into a namespace of my choosing?

Second, what if I wanted to use two different libraries which both defined a non-namespaced Logger. Is there any way of avoid a namespace conflict in this case?

Svaberg
  • 1,501
  • 1
  • 19
  • 40
  • 2
    You can force it into a namespace easily enough, but then the library won't link anymore. Use a telephone instead, ask the library author to fix this. – Hans Passant Feb 20 '16 at 12:23
  • 2
    What kind of library are you dealing with? headers only? access to source code? precompiled? – Anedar Feb 20 '16 at 12:24
  • If you have a source - add the `namespace` where appropriate. If precompiled - maybe, wrap `#include` into `using namespace ...` would help? (I am not sure, please explain, who knows, would it work?) – John_West Feb 20 '16 at 12:29
  • 3
    Fix the root problem. The library forgot to use namespaces. Have them fix it. – BitTickler Feb 20 '16 at 12:29
  • 1
    @John_West: No, don't wrap the include if its precompiled. It will just lead to unresolved external symbols since the library was compiled without a namespace. – Anedar Feb 20 '16 at 12:31
  • Remind library author [about this saying](http://stackoverflow.com/questions/876089/who-wrote-this-programing-saying-always-code-as-if-the-guy-who-ends-up-maintai). – Revolver_Ocelot Feb 20 '16 at 12:33
  • 2
    *"My project's symbols are also not namespaced."* - I don't understand this. You experience first-hand how names in the global namespace are causing trouble, yet you still refuse to do it correctly yourself?! – Christian Hackl Feb 20 '16 at 12:40
  • Thanks for the comments! It seems a social approach is favored :-) @ChristianHackl my code is namespaced; the question was meant to lead up to the second part about two non-namespaced libraries. Sorry for wasting your time. I edited the question to make this more clear. – Svaberg Feb 21 '16 at 00:24

2 Answers2

5

Is there any way that I can "force" the library's symbols into a namespace of my choosing?

No, you can't really do that. A way I could imagine is to provide a wrapper API that actually uses a namespace and forwards from different names like LoggerExt.

What if I wanted to use two different libraries which both defined a non-namespaced Logger?

You are lost, at least the linker will tell you so.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
0

You have 4 Options from what I can tell:

  • Make sure all of your code is within a namespace. (This should be a given)
  • Contact the developers of the specified library letting them know about it waiting and hoping for an updated version. (Proper communication and having patience)
  • Rewrite the entire library yourself encapsulating it fully within a namespace. (Good luck with this, unless you know what you are doing)
  • Find a new, better library that is comparable to the one you are currently using that will provide all the functionality that you need that is well designed. (Lots of research, homework and reading involved, but may pay off in the end)
Francis Cugler
  • 7,788
  • 2
  • 28
  • 59