llvmcontext作为类成员断路构造函数

LLVMContext as class member breaks constructors?

本文关键字:断路 构造函数 成员 llvmcontext      更新时间:2023-10-16

我正在尝试在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认为我试图使用一个参数的构造函数("但是提供了1个")?

llvm::LLVMContext不是可复制的类。它是从文档中删除的副本。

LLVMContext (LLVMContext &) = delete

由于您确实复制了初始化,因此编译器必须检查您的班级有一个可行的副本。但这是由于llvm::LLVMContext的隐式删除。

除非您使用的是C 17,否则保证了复印件并可以避免支票,只需摆脱auto类型声明:

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