简体   繁体   English

llvm :: Module是两个运算符。*和-> *的左侧

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

I was am reading LLVM Tutorial and saw these statements (at different positions): 我正在阅读LLVM教程,并看到了以下这些语句(在不同的位置):

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

But when I try putting this in my code: 但是,当我尝试将其放入代码中时:

TheModule->get();

I get error: 我得到错误:

myTest.cpp:116:16: error: no member named 'get' in 'llvm::Module' TheModule->get(); myTest.cpp:116:16:错误:'llvm :: Module'中没有名为'get'的成员TheModule-> get();

Why could llvm::Module be the left side of both .* and ->* ? 为什么llvm::Module可以同时是.*->*的左侧? And why does TheModule.get() work, but TheModule->get() doesn't? 为什么TheModule.get()工作,但是TheModule->get()却不能呢?

Does this have anything to do with std::unique_ptr ? 这和std::unique_ptr吗?

Does this have anything to do with std::unique_ptr? 这和std :: unique_ptr有关系吗?

Yes, it does. 是的,它确实。

It's the std::unique_ptr 's get you managed to call with TheModule.get() , which returns the managed pointer, TheModule->get() is not the same, it's equivalent to: 这是std::unique_ptr ,可get您通过TheModule.get()进行调用,该方法返回托管指针, TheModule->get()不相同,它等效于:

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

These are all the same and they call get on the managed llvm::Module instance. 这些都是一样的,它们在托管的llvm::Module实例上调用get Of course, llvm::Module doesn't have get , that's why you're getting the error. 当然, llvm::Module没有get ,这就是为什么出现错误的原因。

It's really not that surprising once you learn what operator* and operator-> of std::unique_ptr do. 一旦学习了std::unique_ptr operator*operator-> ,这并不奇怪。 These operators exist, so you can work with smart pointers (syntactically) the same way as with raw ones. 这些运算符存在,因此您可以像处理原始指针一样,在语法上使用智能指针。 And std::unique_ptr::get returns a raw pointer to the managed object. 并且std::unique_ptr::get返回指向托管对象的原始指针。

When you use the "arrow" operator -> you access the contained pointer, when you use the dot operator . 使用“ arrow”运算符->时,使用点运算符时将访问包含的指针. you access the unique_ptr object. 您访问unique_ptr对象。

For example 例如

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM