【问题标题】:LLVMContext as class member breaks constructors?LLVMContext 作为类成员会破坏构造函数吗?
【发布时间】:2017-09-03 11:00:01
【问题描述】:

我正在尝试在 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”)?

【问题讨论】:

    标签: c++ clang llvm


    【解决方案1】:

    llvm::LLVMContext 不是可复制的类。它的copy c'tor被删除了,从documentation:

    LLVMContext (LLVMContext &) = delete
    

    由于您进行了复制初始化,编译器必须检查您的类是否存在可行的复制c'tor。但是由于llvm::LLVMContext,它被隐式删除了。

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

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

    【讨论】:

    • 天哪。谢谢。我非常专注于在类定义中发现错误,我完全忽略了这一点。
    猜你喜欢
    • 2014-08-04
    • 1970-01-01
    • 2014-11-23
    • 1970-01-01
    • 1970-01-01
    • 2016-06-14
    • 2019-07-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多