2017-09-03 106 views
2

我正在嘗試在類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個」)?

回答

4

llvm::LLVMContext不是可複製的類。這是一份c'tor被刪除,從documentation

LLVMContext (LLVMContext &) = delete 

你既然複製的初始化,編譯器檢查有相關類的可行的副本c'tor。但是由於llvm::LLVMContext的原因,它被隱式刪除。

除非你使用C++ 17在那裏拷貝省音保證和編譯器可以避開檢查,剛剛擺脫了auto類型聲明:

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

天哪。謝謝。我非常想到在類定義中發現一個錯誤,我完全忽略了這一點。 – idmean

相關問題