2

I want to include std::unique_lock and some other names from the <mutex> header, but not std::mutex and other names (which I have a different declaration of - in Relacy Race Detector if you're curious). Which is causing compile errors. How can I do this?

Edit: The compiler errors which occur when RRD and <mutex> are included are:

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
Eloff
  • 20,828
  • 17
  • 83
  • 112
  • 2
    Do you have to have your mutex in std's namespace? – Pubby Mar 04 '12 at 20:22
  • 1
    Did you use `using namespace std;`? Then all members of `std` are introduced in the current scope and you can't un-introduce them. Also provide your compiler errors, please. And are you sure, that the RRD mutex is in `std` too? – Zeta Mar 04 '12 at 20:23
  • Ah, I see the confusion, no it's in namespace std, but the RRD mutex is also in std (because it's designed to be a drop in replacement for std atomics, mutex and others) but it doesn't include everything like std::unique_lock. – Eloff Mar 04 '12 at 20:31

1 Answers1

3

You could #include RRD into its own namespace :

namespace RRD {
  #include <rrd.h>
}

This should work unless RRD refers to global-namespace functions (e.g., ::isalnum).

Then, import classes into your current namespace:

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

Finally, use mutex in your code, not std::mutex. If you're concerned with things living in the global namespace, you can wrap the above code in its own namespace.

user1071136
  • 15,636
  • 4
  • 42
  • 61