llvm::Module 是运算符 .* 和 ->* 的左侧

llvm::Module be the left side of both operator .* and ->*

我正在阅读 LLVM Tutorial 并看到了这些陈述(在不同的位置):

static std::unique_ptr<Module> TheModule;
TheModule.get();
TheModule->getFunction(arg);

但是当我尝试将其放入我的代码中时:

TheModule->get();

我收到错误:

myTest.cpp:116:16: error: no member named 'get' in 'llvm::Module' TheModule->get();

为什么 llvm::Module 可以是 .*->* 的左边?为什么 TheModule.get() 有效,而 TheModule->get() 无效?

这跟std::unique_ptr有关系吗?

Does this have anything to do with std::unique_ptr?

是的,确实如此。

这是你用TheModule.get()调用的std::unique_ptrget,returns托管指针,TheModule->get()不一样,相当于:

TheModule.get()->get();
(*TheModule).get();
(*TheModule.get()).get();

这些都是一样的,它们在托管 llvm::Module 实例上调用 get。当然,llvm::Module 没有 get,这就是您收到错误的原因。

一旦您了解了 operator* and operator-> of std::unique_ptr do. These operators exist, so you can work with smart pointers (syntactically) the same way as with raw ones. And std::unique_ptr::get returns 指向托管对象的原始指针,这就真的不足为奇了。

当您使用 "arrow" 运算符 -> 时,您访问包含的指针,当您使用点运算符 . 时,您访问 unique_ptr 对象。

例如

TheModule.get()  // Calls std::unique_ptr<T>::get()

TheModule->getFunction(arg);  // Calls llvm::Module::getFunction()