简体   繁体   English

为什么我们需要传递 const 共享指针作为引用?

[英]Why do we need to pass const shared pointer as reference?

While I was reading an open-source code, I found the following where a shared pointer is passed to a function in two different ways.在阅读开源代码时,我发现以下内容以两种不同的方式将共享指针传递给函数。

class A{A();};

typedef std::shared_ptr<A> A_Ptr;

void func1(A_Ptr a);

void func2( const A_Ptr& a);

What is the reason to pass it as a reference when const is used?使用 const 时将其作为引用传递的原因是什么? I understand the writer of the func2 does not expect the function to be able to change anything in a.我了解 func2 的作者不希望该函数能够更改 a.c 中的任何内容。 But can't we just do this const A_Ptr a ?但是我们不能只做这个const A_Ptr a吗? Also, what are the reason we shouldn't pass it with A_Ptr & a in func1?另外,我们不应该在 func1 中使用A_Ptr & a传递它的原因是什么?

Occurance of the above in the code:代码中出现上述情况:

Git: https://github.com/uzh-rpg/rpg_svo/ Git: https : //github.com/uzh-rpg/rpg_svo/

List of FramePtr in the code 代码中FramePtr列表

void FrameHandlerMono::setFirstFrame(const FramePtr& first_frame) void FrameHandlerMono::setFirstFrame(const FramePtr& first_frame)

void DepthFilter::addFrame(FramePtr frame) void DepthFilter::addFrame(FramePtr frame)

But can't we just do this const FramePtr frame?但是我们不能只做这个const FramePtr框架吗?

Sure, we could, but then the copy constructor would be invoked, which is for larger types (anything larger than the built-in types) normally more expensive than passing by reference.当然,我们可以,但是随后会调用复制构造函数,这对于较大的类型(任何大于内置类型的类型)通常比通过引用传递更昂贵。 This is nothing specific to shared_ptr .这不是shared_ptr特有的。 It should be generally your default to pass any objects by const reference if you don't need a copy and don't want to change them.如果您不需要副本并且不想更改它们,那么通过 const 引用传递任何对象通常应该是您的默认设置。 Only built-in types like int, float, or char should be passed-by-value.只有像 int、float 或 char 这样的内置类型才应该按值传递。

More interesting is why func1 uses a copy.更有趣的是为什么func1使用副本。 Most probable case is that he needs a copy anyway, because he wants to keep a reference in the class.最可能的情况是他无论如何都需要一个副本,因为他想在类中保留一个引用。 I couldn't find the exact file you're refering to in the github repository you've posted.我在您发布的 github 存储库中找不到您引用的确切文件。 If it's still unclear please past the function body of func1 into the question.如果还不清楚,请将 func1 的函数体传递到问题中。

Edit: Ah, I see.编辑:啊,我明白了。 Looks like the reason he passes-by-value here, has more to do with thread-safety.看起来他在这里传递值的原因更多地与线程安全有关。 Didn't read the whole but otherwise the shared_ptr might be deleted by the owning thread if he passed by const reference.没有阅读整个内容,但如果他通过常量引用传递,则拥有的线程可能会删除shared_ptr

Here for examle func needs pass-by-value cause otherwise the pointer could be deleted by the main thread.例如, func 需要按值传递,否则主线程可能会删除指针。 Probably something like this but more complicated:可能是这样的,但更复杂:

#include <chrono>
#include <iostream>
#include <memory>
#include <thread>

using namespace std::chrono_literals;

struct S {
    S() {}
};

void
//This signature would be false
//func(std::shared_ptr<S> const& s)
func(std::shared_ptr<S> s)
{
    std::cout << s.use_count() << '\n';
    std::this_thread::sleep_for(2s);
    //use_count would be 0 here if we pass by reference
    std::cout << s.use_count() << '\n';
}



int
main(int argc, char**) {
    std::shared_ptr<S> s{std::make_shared<S>()};

    std::thread t{func, std::ref(s)};

    std::this_thread::sleep_for(1s);

    s.reset();

    t.join();

    return 0;
}

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

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