转发没有类声明的shared_ptr [英] Forwarding a shared_ptr without class declaration

查看:144
本文介绍了转发没有类声明的shared_ptr的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意:我发现错误的来源实际上不是与shared_ptr相关,只是聪明地伪装成错误消息。

NOTE: I've found the source of the error is not actually related to the shared_ptr, just cleverly disguised as such in the error message. Thus the below is basically nonsense (not the answers, they're fine)

-

我是这样的, m有一些麻烦使用 shared_ptr (boost的在这一刻),我需要简单地转发一个指针到另一个函数。使用本地指针,中间函数不需要访问类的定义,但使用smart_ptr的它看起来它。有没有办法避免这种情况?

I'm having some trouble using a shared_ptr (boost's at the moment) where I need to simply forward a pointer to another function. Using native pointers the intervening function would not need to have access to the definition of the class, but using smart_ptr's it appears it does. Is there any way to avoid this?

例如,给定目标函数:

void func( shared_ptr<SomeClass> const & obj )

$ c> const& 负责处理部分问题,但是我们有一个getter类,获取其他类的对象,如:

The const & takes care of part of the problem, but say we have a getter class which obtains the object for some other class, like:

shared_ptr<SomeClass> someClassInstance();

这里是我想简单地组合参数并转发到目标函数: p>

And here is where I'd like to simply assemble arguments and forward to the target function:

func( someClassInstance() );

使用普通指针,代码中的这一点可以简单地使用 SomeClass ,但是有一个 smart_ptr 它需要有完整的定义(可能是smart_ptr可能需要删除类)。

With a plain pointer this point in the code could simply use a forward declaration of SomeClass, but with a smart_ptr it needs to have the full definition (presumably as the smart_ptr might need to delete the class).

现在,如果 someClassInstance 要返回 const& 问题实际上会消失,因为插入代码不会复制任何对象。但是,由于线程安全的原因,getter函数必须返回副本。

Now, if someClassInstance were to return a const & this problem would actually go away as the intervening code would not be copying any objects. However, the getter function must return the copy for thread-safety reasons.

有没有我可以实现这种类型的智能指针参数转发而不需要类定义?也就是说,我可以在这种情况下使用与传统指针相同的智能指针。

Is there anyway I can achieve this type of smart pointer parameter forwarding without needing the class definition? That is, can I use smart pointers in the same fashion as I would a traditional pointer in this circumstance.

-

UPDATE :写一个小测试,答案是正确的,前向声明是足够的。然而,海湾合作委员会仍然抱怨在一种情况。我将不得不弄清楚导致它失败的原因(在这种特殊情况下)。

UPDATE: Writing a small test the answers are correct that a forward declaration is enough. Yet GCC is still complaining in one situation. I'm going to have to figure out exactly what is causing it to fail (in this particular situation).

现在我关闭这个问题,或者什么?

推荐答案

您需要至少一个 T 每次提及 shared_ptr< T>

You need at least a forward declaration for T for every mention of shared_ptr<T>.

仅当使用一元 shared_ptr :: operator * shared_ptr :: operator- > ,需要完整的东西。在引擎盖下, shared_ptr 使用compiletime和运行时多态性的混合,使这成为可能。另请参见此问题,了解魔法。

Only if you use unary shared_ptr::operator* and shared_ptr::operator->, the full thing is needed. Under the hood, shared_ptr uses a mix of compiletime- and runtime-polymorphism, making this possible. See also this question to learn about the "magic".

示例:

// frob.h
#ifndef FROB_H
#define FROB_H

#include <shared_ptr>

class Foo;
void grind (std::shared_ptr<Foo>);

#endif

请注意,规范方式 shared_ptr 是按值(即删除 const& )。

Note that the canonical way to pass shared_ptr is by value (i.e. remove the const&).

这篇关于转发没有类声明的shared_ptr的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆