什么时候提高::绑定投参数所需要的类型? [英] When does boost::bind cast arguments to the required type?

查看:93
本文介绍了什么时候提高::绑定投参数所需要的类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用boost ::绑定到参数绑定到一个功能 - 当他们在铸造由功能需要的类型(如果隐式转换是可能的)

When I use boost::bind to bind parameters to a function - when are they casted to the type required by the function (if an implicit cast is possible)?

他们是如何存储在bind_t对象吗?作为类型最初传递绑定或由函数签名所需要的类型?

How are they stored in the bind_t object? As the type originally passed to bind or as the type required by the function signature?

具体做法是:

如果我有签名的功能

void SomeFun(SmartPointer<SomeType>)

和我使用绑定为

boost::bind(&SomeFun, somePtr)

其中, somePtr 的类型为 SOMETYPE * ,将在 bind_t 对象包含副本 somePtr 存储为一个简单的指针或将它铸造于智能指针&LT; SOMETYPE&GT; 和存储为智能指针&LT;&SOMETYPE GT;

where somePtr is of type SomeType*, will the bind_t object contain a copy of somePtr stored as a simple pointer or will it be casted to the SmartPointer<SomeType> and be stored as a SmartPointer<SomeType>?

有从 SOMETYPE * 隐式转换为智能指针&LT;&SOMETYPE GT; 。相对于的boost :: shared_ptr的智能指针使用引用计数器的管理对象,这意味着 SOMETYPE 必须从 SmartPointed 。

There is an implicit cast from SomeType* to SmartPointer<SomeType>. As opposed to boost::shared_ptr this SmartPointer uses a reference counter in the managed objects, meaning SomeType has to be derived from SmartPointed.

推荐答案

这甚至不会工作,因为没有隐式转换或隐式构造函数,SOMETYPE shared_ptr的*。

This won't even work as there is no implicit conversion or implicit constructor to shared_ptr from SomeType*.

您应该叫

boost::bind(&SomeFun, boost::shared_ptr<SomeType>(somePtr))

如果somePtr是当shared_ptr的的最后一个引用超出范围稍后删除你刚才分配的新,并期望一个指针。如果您不想指针被删除,但你知道它会仍然有效的通话时间,函数必须shared_ptr的,你可以使用无操作的删除创建的shared_ptr。无论哪种方式,这是一个shared_ptr,而不是一个指针或weak_ptr的或其他任何东西,你必须通过在这种情况下。

if somePtr is a pointer you have just allocated with "new" and expect to be deleted later when the last reference of the shared_ptr goes out of scope. If you don't want the pointer to be deleted but you know it will still be valid at the time of the call, and the function must take shared_ptr, you can use a no-op deleter to create the shared_ptr. Either way, it is a shared_ptr, not a pointer or a weak_ptr or anything else you must pass in in this instance.

你说你的情况是不同的,所以我们必须看到您的实际情况,或一个匹配的更接近。

You say your case is different so we would have to see your actual case or one that matches it closer.

您可能会感到困惑与在那里你传递的功能是一个类的成员函数的情况下,你的类实例(对象)作为参数之一传递。在这里,您可以传递一个指针,引用或一个shared_ptr,它可以是一个const引用如果函数是一个const法(类似指针到const或shared_ptr的为const)。

You may be getting confused with the case where the function you pass in is a class member function and you pass in the class instance (the object) as one of the parameters. Here you can pass in a pointer, a reference or a shared_ptr and it can be a const reference if the function is a const-method (similarly pointer-to-const or shared_ptr to const).

这很简单,因为有不同的重载提振::所有这些绑定当函数是一个类的成员函数。

That is simply because there are different overloads to boost::bind for all these when the function is a class member function.

当转换是隐式的隐式转换将在函数被调用的时候发生。提高::绑定就是这样存储的东西传递到它的模板。
如果它是用来调用成员函数一些魔术将与第一参数发生

Where the conversion is implicit the implicit conversion will happen at the time the function is called. boost::bind is just a template that stored what is passed into it. Some magic will occur with the first parameter if it is used to call a member function.

请注意,有时刺激::绑定将存储一个boost ::裁判那里的功能,其实需要一个参考。

Note that sometimes boost::bind will store a boost::ref where the function actually takes a reference.

这篇关于什么时候提高::绑定投参数所需要的类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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