Search code examples
c++boostboost-bind

When does boost::bind cast arguments to the required type?


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)?

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?

Specifically:

If I have a function of the signature

void SomeFun(SmartPointer<SomeType>)

and I use bind as

boost::bind(&SomeFun, somePtr)

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>?

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.


Solution

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

    You should call

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

    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.

    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.

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