【问题标题】:Resolve include conflicts (re-definitions) or how to include declared members selectively解决包含冲突(重新定义)或如何选择性地包含声明的成员
【发布时间】:2012-03-04 20:14:14
【问题描述】:

我想包含 std::unique_lock 和 标头中的一些其他名称,但不包含 std::mutex 和其他名称(如果你好奇的话,我在 Relacy Race Detector 中有不同的声明) .这导致编译错误。我该怎么做?

编辑:包含 RRD 和 时发生的编译器错误是:

error C2371: 'rl::condition_variable' : redefinition; different basic types C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\mutex    58  1   test_hashtable
error C2371: 'rl::mutex' : redefinition; different basic types  C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\mutex    100 1   test_hashtable
error C2371: 'rl::recursive_mutex' : redefinition; different basic types    C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\mutex    114 1   test_hashtable

【问题讨论】:

  • 你必须在 std 的命名空间中有你的互斥锁吗?
  • 你用过using namespace std;吗?然后std 的所有成员都被引入到当前作用域中,你不能取消他们的引入。还请提供您的编译器错误。您确定 RRD 互斥锁也在 std 中吗?
  • 啊,我看到了混乱,不,它在命名空间 std 中,但 RRD 互斥体也在 std 中(因为它被设计为替代 std 原子、互斥体和其他)但它没有'不包括 std::unique_lock 之类的所有内容。

标签: c++


【解决方案1】:

你可以将#includeRRD 放到它自己的命名空间中:

namespace RRD {
  #include <rrd.h>
}

这应该可以工作,除非 RRD 引用全局命名空间函数(例如,::isalnum)。

然后,将类导入您当前的命名空间:

#ifdef DETECT_RACE_CONDITIONS
using RRD::std::mutex;
#else
using std::mutex;
#end
using std::unique_lock;

最后,在你的代码中使用mutex,而不是std::mutex。如果你关心全局命名空间中的东西,你可以将上面的代码包装在它自己的namespace 中。

【讨论】:

  • 好的,但是现在我需要使用RLD中的互斥锁来替换std::mutex等,我该怎么做呢?
  • 添加了同时使用 RRD 和 STD 的可能方式。
  • 如果 RRD 不只是标头,这将导致很多问题:编译后的符号位于命名空间 std,而不是 RRD,因此链接器将找不到它们。见这里:stackoverflow.com/a/6670753/46642
猜你喜欢
  • 2014-08-15
  • 2020-07-29
  • 1970-01-01
  • 2013-11-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-18
  • 2020-04-27
相关资源
最近更新 更多