我有一个 Visual Studio 项目,其中包含托管代码文件和非托管代码文件。该项目具有 CLR 支持,但是当我在不需要 .NET 的地方添加文件时,我只需右键单击该文件即可关闭/crl 选项:
我添加了一个必须包含非托管代码并使用 std::mutex 的类。
// Foo.h
class Foo
{
std::mutex m;
}
编译后出现如下错误:
error C1189: #error : is not supported when compiling with /clr or /clr:pure.
问题是我没有关闭头文件 (.h) 的 clr 的选项,因为这是我右键单击 .h 文件时的窗口:
我该如何解决这个问题?
最佳答案
可以使用称为 Pointer To Implementation (pImpl) idiom 的解决方法.
下面是一个简单的例子:
// 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())
{
}
关于c++ - 使用 std::mutex 关闭头文件的 clr 选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31359179/