LLVMContext 作为 class 成员破坏了构造函数?

LLVMContext as class member breaks constructors?

我正在尝试在 class Application 中创建一个 LLVMContext 成员变量。 MCVE:

#include <llvm/IR/LLVMContext.h>

struct Foo {};

class Application {
public:
  Application(int a, Foo foo, int b);
private:
  llvm::LLVMContext context_;
};

void function() {
  auto application = Application(12, Foo(), 21);
}

但是,添加变量会产生一些非常奇怪的错误:(Clang 4.0.1 和 Apple LLVM 版本 8.1.0)

toy.cpp:13:8: error: no matching constructor for initialization of 'Application'
  auto application = Application(12, Foo(), 21);
       ^             ~~~~~~~~~~~~~~~~~~~~~~~~~~
toy.cpp:5:7: note: candidate constructor (the implicit copy constructor) not viable: expects an l-value
      for 1st argument
class Application {
      ^
toy.cpp:7:3: note: candidate constructor not viable: requires 3 arguments, but 1 was provided
  Application(int a, Foo foo, int b);
  ^
1 error generated.

这是怎么回事?为什么 Clang 认为我在尝试使用带有一个参数 ("but 1 was provided") 的构造函数?

llvm::LLVMContext 不是可复制的 class。它的副本 c'tor 已删除,来自 documentation:

LLVMContext (LLVMContext &) = delete

由于您进行了复制初始化,编译器必须检查您的 class 是否存在可行的复制 c'tor。但由于 llvm::LLVMContext.

而被隐式删除

除非您使用的是 C++17,其中保证复制省略并且编译器可以避免检查,否则只需删除 auto 类型声明:

Application application {12, Foo(), 21};