10

I have a Visual Studio project that contains files with managed code and files with unmanaged code. The project has the CLR support, but when I add a file where I do not need .NET I simply turn off the /crl option with a right-click on the file:

enter image description here

I added a class that has to contain unmanaged code and use std::mutex.

// Foo.h
class Foo
{
   std::mutex m;
}

I got the following error after compiling:

error C1189: #error : is not supported when compiling with /clr or /clr:pure.

The problem is that I do not have the option to turn off the clr for header files (.h), since this is the window when i right-click on a .h file:

enter image description here

How can I fix this problem?

Nick
  • 10,309
  • 21
  • 97
  • 201
  • My crystal ball says that it was not a good idea to put this class in a .h file. Because you are also #including it an a .cpp file that is getting compiled with /clr in effect. Avoid exposing class internals with an interface. – Hans Passant Jul 11 '15 at 16:23
  • @HansPassant yes, I have the .cpp file that includes Foo.h. Where **exaclty** should I move all the classes included in cpp files where I disabled the clr option? – Nick Jul 11 '15 at 16:28

1 Answers1

9

There is the possibility to use the workaround known as the Pointer To Implementation (pImpl) idiom.

Following is a brief example:

// Foo.h
#include <memory>

class Foo
{
public:

  Foo();

  // forward declaration to a nested type
  struct Mstr;
  std::unique_ptr<Mstr> impl;
};


// Foo.cpp
#include <mutex>

struct Foo::Mstr
{
  std::mutex m;
};

Foo::Foo()
  : impl(new Mstr())
{
}
gliderkite
  • 8,828
  • 6
  • 44
  • 80