如何将shared_ptr作为参数传递给可变对象?

发布于 2024-12-19 23:45:24 字数 667 浏览 3 评论 0原文

我想通过智能指针引用将对象传递给函数。该函数可以更改引用对象的值,但不能更改引用本身。有两种明显的方法可以处理这个问题。

第一种按值传递 shared_ptr 的方法 - 它是引用,因此本身不需要按引用传递。明显的问题是引用的复制,这意味着一些引用计数开销。

void foo (shared_ptr<bar> p)

第二种方法是通过const引用传递shared_ptr - 避免复制shared_ptr实例,而是意味着对引用对象的访问需要两层解引用,而不是一层。

void foo (const shared_ptr<bar> &p)

在实践中,这些理论开销通常是微不足道且无关紧要的。这对我来说,我应该几乎总是遵循一些标准约定,而不是为每个具体情况选择一种方法或另一种方法。这就引出了一个问题......

对于我通常应该选择哪些方法是否有一个标准约定?如果是这样,哪个是常规选择?

编辑 - 可能值得一提 - 考虑传递常量引用情况的一个原因是因为有一个预先存在的约定,即大多数类/结构实例是通过常量引用而不是通过值传递,并且 shared_ptr 是一个类。当然,它不是一个重量级的类(复制的成本很小),因此旧约定背后的原因可能不适用。

I want to pass an object by smart-pointer reference to a function. The function may change the value of the referenced object, but may not change the reference itself. There are two obvious ways to handle this.

The first approach to pass the shared_ptr by value - it is the reference, so doesn't itself need to be passed by reference. The apparent issue with this is copying of the reference, which suggests some reference-counting overhead.

void foo (shared_ptr<bar> p)

The second approach is to pass the shared_ptr by const reference - avoiding the copying of the shared_ptr instance, but instead implying that accesses to the referenced object need two layers of dereferencing instead of one.

void foo (const shared_ptr<bar> &p)

In practice, these theoretical overheads will normally be trivial and irrelevant. Which suggests to me that instead of choosing one approach or the other for each individual case, I should almost always follow some standard convention. Which leads to the question...

Is there a standard convention for which of these approaches I should normally choose? If so, which is the conventional choice?

EDIT - Probably worth mentioning - one reason to consider the pass-by-const-reference case is because there's a pre-existing convention that most class/struct instances are passed by const-reference rather than by value, and shared_ptr is a class. Of course it isn't a heavyweight class (the cost of copying is small), so the reasons behind that older convention may not apply.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

空‖城人不在 2024-12-26 23:45:24

始终按值传递 shared_ptr。如果传递引用,则可能会遇到这样的问题:对 shared_ptr 管理的对象的函数的调用可能会重置它,现在您就得到了一个悬空指针。如果按值传递,则可以确保该对象在当前函数调用中继续存在。

请参阅此处了解更多信息。


例如:

#include <memory>
#include <iostream>

std::shared_ptr<int> ptr(new int(42));

void foo(){
  ptr.reset();
}

void bar(std::shared_ptr<int> const& p){
  foo();
  std::cout << *p;
}

int main(){
  bar(ptr);
}

这应该与少许盐一起服用。它可以用来证明任何类型都不应该通过 const-reference 传递 - 例如参见 http://ideone.com /1IYyC,Benjamin Lindley 在评论中指出。

然而,在实践中,此类问题确实会偶然出现更复杂的变化。例如,这就是为什么我们被警告迭代器(以及常量引用返回值)会因改变引用容器的方法而失效的原因。一般来说,这些规则很容易遵循,但有时更间接和意想不到的例子可能会让人大吃一惊。

既然如此,最好在不需要时避免额外的引用层。

Always pass shared_ptrs by value. If you pass a reference, you might run into the problem that a call to a function of the object managed by the shared_ptr might just reset it, and now you've got a dangling pointer. If you pass by value, you ensure that the object will survive the current function call.

See here for much, much more information.


Example:

#include <memory>
#include <iostream>

std::shared_ptr<int> ptr(new int(42));

void foo(){
  ptr.reset();
}

void bar(std::shared_ptr<int> const& p){
  foo();
  std::cout << *p;
}

int main(){
  bar(ptr);
}

This should be taken with a pinch of salt. It can be adapted to prove that no type should ever be passed by const-reference - see for example http://ideone.com/1IYyC, which Benjamin Lindley pointed out in the comments.

However, more complex variations of this kind of issue do arise by accident in practice. This is the reason, for example, why we are warned that iterators (as well as const-reference return values) are invalidated by methods that mutate the referenced container. These rules are easy enough to follow in general, but occasionally more indirect and unexpected examples can catch people by surprise.

That being the case, it's best to avoid the extra layer of referencing when it's not needed.

-柠檬树下少年和吉他 2024-12-26 23:45:24

如果函数操作对象,则如果对象包含在智能指针中,则该函数不应该相关。它应该接受 T&。一些人认为操纵值的自由函数是不好的风格。通过引用 const 获取对象并返回新值可以更干净。

If the function manipulating the object it should not be relevant if the object is contained in a smart pointer. It should accept a T&. Free functions that manipulate values are considered bad style by some. Taking the object by reference to const and returning a new value can be cleaner.

×眷恋的温暖 2024-12-26 23:45:24

我一直路过 const&基于这样的想法:调用堆栈上的某个地方应该有人持有shared_ptr的副本,因此它的生命周期在我的整个执行过程中得到保证。

如果您在不同的线程上执行,则需要一个副本 - 就此而言,任何存储shared_ptr(不仅仅是在其方法期间使用它)都需要存储一个副本,不作为参考。

而且,既然我已经写了,我将去读一下为什么人们似乎支持相反的心态。

I've been passing by const& based on the idea that somewhere up the call stack someone should be holding a copy of the shared_ptr and therefore its life is guaranteed for my entire execution.

In the case that you are executing on a different thread, you need a copy - and for that matter anyone that stores the shared_ptr (not just uses it for the duration of their method) needs to store a copy, not a reference.

And, now that I've written that I'll go and read why people seem to be supporting the opposite mentality.

╰◇生如夏花灿烂 2024-12-26 23:45:24

@Xeo 所说的一个例子:

void foo(shared_ptr<bar> &p)
{
    p.reset(); // ref_count == 0; memory is freed.
}

shared_ptr<bar> p(new bar); // ref_count == 1
foo(p);

const shared_ptr不会发生这种情况。 &p

An example to what @Xeo said:

void foo(shared_ptr<bar> &p)
{
    p.reset(); // ref_count == 0; memory is freed.
}

shared_ptr<bar> p(new bar); // ref_count == 1
foo(p);

This does not happen for const shared_ptr<bar> &p.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文