LLVMContext作为类成员会破坏构造函数吗?
问题描述:
我正在尝试在类Application
中创建一个LLVMContext
成员变量。 MCVE:LLVMContext作为类成员会破坏构造函数吗?
#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);
}
添加变量,但是,产生一些非常奇怪的错误:(4.0.1锵和苹果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
不是可复制的类。这是一份c'tor被删除,从documentation:
LLVMContext (LLVMContext &) = delete
你既然复制的初始化,编译器检查有相关类的可行的副本c'tor。但是由于llvm::LLVMContext
的原因,它被隐式删除。
除非你使用C++ 17在那里拷贝省音保证和编译器可以避开检查,刚刚摆脱了auto
类型声明:
Application application {12, Foo(), 21};
天哪。谢谢。我非常想到在类定义中发现一个错误,我完全忽略了这一点。 – idmean