Search code examples
c++boostconstantsshared-ptr

convert const std::shared_ptr<const T> into boost::shared_ptr<T>


I need convert a variable type of const std::shared_ptr<const T> into boost::shared_ptr<T>.

In the following scanCallback(), I can not modify the param const std::shared_ptr<const io_adaptor::msg::PandarScan> msg. The msg is very big in memory, which contains large lidar points. PushScanPacket() func's arg is boost::shared_ptr<io_adaptor::msg::PandarScan>, which I also can not modify its type. The following code does not compile successfully, does somebody know how to do this?

void HesaiLidarModule::scanCallback(
    const std::shared_ptr<const io_adaptor::msg::PandarScan> msg){
    std::remove_const<const std::shared_ptr<
                            const io_adaptor::msg::PandarScan> 
                     >::type non_const_msg(msg);
    boost::shared_ptr<io_adaptor::msg::PandarScan> msg_boost(non_const_msg.get());
    hsdk->PushScanPacket(msg_boost);
}

UPDATE_1st: The following code can compile successfully, but i'm not sure whether std::remove_const<const io_adaptor::msg::PandarScan>::type non_const_obj(*msg); induces a copy operator, which is expensive for msg.

void HesaiLidarModule::scanCallback(
    const std::shared_ptr<const io_adaptor::msg::PandarScan> msg){
    std::remove_const<const io_adaptor::msg::PandarScan>::type non_const_obj(*msg);
    io_adaptor::msg::PandarScan& copy = non_const_obj;          
    boost::shared_ptr<io_adaptor::msg::PandarScan> msg_boost(&copy);
    hsdk->PushScanPacket(msg_boost);
}

Solution

  • You cannot transfer ownership from std::shared_ptr to boost::shared_ptr. You might from a std::unique_ptr though.

    But you can create a boost::shared_ptr with a custom deleter.

    boost::shared_ptr<io_adaptor::msg::PandarScan>
        msg_boost(const_cast<io_adaptor::msg::PandarScan*>(msg.get()),
                  [msg = msg](auto*) mutable { msg.reset(); });
    

    Demo

    Deleter captures original shared_ptr to maintain the lifetime, and instead of releasing the resource, it just "decreases" the refcount.