【问题标题】:Turn off clr option for header file with std::mutex使用 std::mutex 关闭头文件的 clr 选项
【发布时间】:2015-07-11 16:18:43
【问题描述】:

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

我添加了一个必须包含非托管代码并使用 std::mutex 的类。

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

编译后出现如下错误:

error C1189: #error : 编译时不支持 /clr 或 /clr:pure。

问题是我没有选择关闭头文件 (.h) 的 clr,因为这是我右键单击 .h 文件时的窗口:

我该如何解决这个问题?

【问题讨论】:

  • 我的水晶球说把这个类放在 .h 文件中不是一个好主意。因为您还 #include 了一个 .cpp 文件,该文件正在使用 /clr 进行编译。避免使用接口暴露类内部。
  • @HansPassant 是的,我有包含 Foo.h 的 .cpp 文件。我应该在哪里 exaclty 将 cpp 文件中包含的所有类移动到我禁用 clr 选项的位置?

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


【解决方案1】:

可以使用称为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())
{
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-28
    • 2013-12-29
    • 2012-11-02
    • 1970-01-01
    • 2017-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多