Boost logo

Boost :

Subject: Re: [boost] Should pass boost::asio::io_service by raw pointer or smart pointer?
From: Antony Polukhin (antoshkka_at_[hidden])
Date: 2018-12-25 12:53:07


On Thu, Dec 20, 2018, 09:08 Richard Hodges via Boost <boost_at_[hidden]
wrote:
<...>

> * Many people keep shared_ptr's to sockets, deadline_timers and so on. This
> is an error. All ASIO io objects are moveable. Store by value.
>

Hmmm... just double checking that my knowledge is up to date: there's no
way to create a timer, async wait for it and pass the ownership to the
callback. In other words, code like the following can not be improved in
C++14:

template <class Time, class Func>
static void run_delayed(Time duration_or_time, const Func& f) {
    auto timer = std::make_unique<boost::asio::deadline_timer>(
        get_ios(), duration_or_time
    );

    boost::asio::deadline_timer& timer_ref = *timer;

    timer_ref.async_wait(
        timer_task<Func>{
            std::move(timer),
            f
        }
    );
}

Not sure about C++17. Is there a way to avoid unique_ptr?


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk