c++ - 使用 std::mutex 关闭头文件的 clr 选项

标签 c++ .net visual-studio c++11 clr

我有一个 Visual Studio 项目,其中包含托管代码文件和非托管代码文件。该项目具有 CLR 支持,但是当我在不需要 .NET 的地方添加文件时,我只需右键单击该文件即可关闭/crl 选项:

enter image description here

我添加了一个必须包含非托管代码并使用 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 文件时的窗口:

enter image description here

我该如何解决这个问题?

最佳答案

可以使用称为 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/

相关文章:

.net - 使用 ML.NET 对文本数组进行 OneHotEncoding

c# - 将自定义信息添加到 CSPROJ 文件

c# - 无法从 Resources.resx 获取新添加的文件

C++ 策略对象和构建器模式

c++ - 如何处理仅适用于 -fconcepts 的参数声明中的 "warning: use of ‘auto’”

c# - 如果等待的任务链上有 'unwrapped'任务,则Task.Result/wait(..)无限期等待,而如果使用 'async/await',则成功完成

c# - Console.In.Peek() 在输入时返回 -1

c# - 统计数组中字符和单词的个数

c++ - 重新定义默认参数: parameter 2

c++ - 给定 c 中的一个固定数,找到所有可能的加法和组合(给定一个和,找到它可能的加法和排列。)