转发一个shared_ptr无类的声明 [英] Forwarding a shared_ptr without class declaration

查看:122
本文介绍了转发一个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)

-

我在使用了一些麻烦的shared_ptr (升压转换器的目前),我需要简单的转发指针给另一个函数。使用本机的指针干预功能就不需要访问类的定义,但使用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?

例如,给定一个目标函数:

For example, given a target function:

void func( shared_ptr<SomeClass> const & obj )

常量和放大器; 利用了问题的一部分照顾,但说我们有一个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();

和这里就是我想简单地组装参数并转发到目标函数:

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

func( someClassInstance() );

通过一个普通的指针,这一点在code可以简单地使用 SomeClass的的正向声明,但有一个 smart_ptr 它需要有完整定义(presumably作为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 是返回一个常量和放大器; 这个问题实际上会消失的干预$ C $ç不会复制任何对象。然而,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.

-

更新的:写一个小的测试答案是正确的,正向的声明是不够的。然而,GCC仍然抱怨在一种情况下。我将不得不弄清楚到底是什么原因造成它失败(在这种特殊情况)。

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的&LT每一个提; T&GT;

只有当你使用一元的shared_ptr ::运算符* 的shared_ptr ::操作符&GT; ,全需要的东西。引擎盖下,的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 是值(即删除常量和放大器; )。

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

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

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