Turn off clr option for header file with std::mutex

一世执手 提交于 2019-12-01 00:12:52

问题


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:

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:

How can I fix this problem?


回答1:


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())
{
}


来源:https://stackoverflow.com/questions/31359179/turn-off-clr-option-for-header-file-with-stdmutex

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!