gnzlbg gnzlbg - 6 months ago 27
Linux Question

Name collision between c++ library namespace and C linux function

The Linux

<ncurses.h>
header defines the function
meta
, and the C++ metaprogramming library
meta
puts all its code in the global namespace
meta
.

How can I use both in the same C++ program? Is there a way to work around the name collision?

I can think of two brittle workarounds, but they are easy to break:


  • Workaround A:

    namespace linux {
    #include <ncurses.h>
    } // namespace linux
    using linux::max_align_t; // ncurses assumes it is in the global namespace
    #include <meta/meta.hpp>


    compiles but will probably fail to link since the
    ncurses
    symbols are expected in the global namespace.

  • Workaround B:

    #include <ncurses.h>
    namespace cpp {
    #include <meta/meta.hpp>
    } // namespace cpp


    is very brittle since it will only work as long as the
    meta
    library doesn't assume that any of its symbols are in the global namespace. That is, if the library needs to disambiguate internally a symbol and uses
    ::meta::symbol_name
    for that, this approach will break.


Answer

I would suggest workaround C: Isolate your code such that the meta library use and the ncurses use are in separate translation units in your project. This way in any particular translation unit there isn't one symbol being used as both a namespace and a global function.