gnz*_*lbg 9 c++ linux namespaces name-collision
Linux <ncurses.h>标头定义了该函数meta,C++元编程库meta将其所有代码放在全局名称空间中meta.
我怎样才能在同一个C++程序中使用它们(不一定是相同的TU,但那会很好)?有没有办法解决名称冲突?
我可以想到两个脆弱的解决方法,但它们很容易破解:
解决方法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>
Run Code Online (Sandbox Code Playgroud)
编译但可能无法链接,因为ncurses符号在全局命名空间中是预期的.
解决方法B:
#include <ncurses.h>
namespace cpp {
#include <meta/meta.hpp>
} // namespace cpp
Run Code Online (Sandbox Code Playgroud)
非常脆弱,因为只有当meta库不假设它的任何符号都在全局命名空间中时它才会起作用.也就是说,如果库需要在内部消除歧义并使用::meta::symbol_name它,那么这种方法就会破坏.
我建议解决方法C:隔离您的代码,以便meta库使用和ncurses使用在项目中的单独翻译单元中.这种方式在任何特定的翻译单元中,没有一个符号被用作命名空间和全局函数.