Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #121977
| From | Paavo Helde <eesnimi@osa.pri.ee> |
|---|---|
| Newsgroups | comp.lang.c++ |
| Subject | Re: How to transfer an allocated object into a function closure |
| Date | 2025-03-14 22:40 +0200 |
| Organization | A noiseless patient Spider |
| Message-ID | <vr2487$21gi7$1@dont-email.me> (permalink) |
| References | <vr20f7$2e5bi$3@gwaiyur.mb-net.net> |
On 14.03.2025 21:36, Marcel Mueller wrote:
> I have allocated objects like GFile* or whatever that are not copyable,
> at least not cheap.
> This objects should be passed to a std::function object that takes the
> ownership.
>
> Normally I would use unique_ptr with move semantics. But for some reason
> the function objects wants to create a copy of the unique_ptr.
>
>
> In fact I allocate the function object on the heap and pass it to
> another thread that takes the ownership.
> It is a GTK application with C++14.
>
> guint gIdleAdd(std::function<void()>* func, gint priority)
> { return g_idle_add_full(
> G_PRIORITY_DEFAULT_IDLE,
> [](void* user_data)
> { (*static_cast<std::function<void()>*>(user_data))();
> return G_SOURCE_REMOVE;
> },
> func,
> [](void* user_data)
> { delete static_cast<std::function<void()>*>(user_data);
> });
> }
>
>
> This works fine:
> (xPtr is a intrusive reference counted smart pointer.)
>
> gIdleAdd(new function<void()>([that = xPtr<ReplayGainWorker>(this),
> error]()
> { that->OnFinished(error); }));
>
>
> But this does not work:
>
> template <typename T, typename D>
> std::unique_ptr<T, D> make_unique(T* ptr, D deleter)
> { return std::unique_ptr<T, D>(ptr, deleter);
> }
>
> GError* error;
> ...
>
> gIdleAdd(new std::function<void()>([error = make_unique(error,
> g_error_free)]()
> { OnDirFailed(nullptr, error.get()); }));
>
>
> g_error_free needs to be invoked exactly once after the function has
> been called or the event is cancelled, i.e. when the function object is
> deleted.
>
In general, passing non-copyable objects to a lambda should be done via
std::move() (requires C++14):
auto u = std::make_unique<FOO>();
bar( [ u = std::move(u) ]() { /*...*/ } );
HTH
Paavo
Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
How to transfer an allocated object into a function closure Marcel Mueller <news.5.maazl@spamgourmet.org> - 2025-03-14 20:36 +0100
Re: How to transfer an allocated object into a function closure Paavo Helde <eesnimi@osa.pri.ee> - 2025-03-14 22:40 +0200
Re: How to transfer an allocated object into a function closure Marcel Mueller <news.5.maazl@spamgourmet.org> - 2025-03-15 10:07 +0100
Re: How to transfer an allocated object into a function closure Bonita Montero <Bonita.Montero@gmail.com> - 2025-03-15 10:34 +0100
Re: How to transfer an allocated object into a function closure Marcel Mueller <news.5.maazl@spamgourmet.org> - 2025-03-16 10:02 +0100
Re: How to transfer an allocated object into a function closure Bonita Montero <Bonita.Montero@gmail.com> - 2025-03-16 14:07 +0100
Re: How to transfer an allocated object into a function closure usenet@stegropa.de (Stefan Große Pawig) - 2025-03-16 18:43 +0100
Re: How to transfer an allocated object into a function closure Bonita Montero <Bonita.Montero@gmail.com> - 2025-03-18 13:18 +0100
Re: How to transfer an allocated object into a function closure usenet@stegropa.de (Stefan Große Pawig) - 2025-03-22 22:44 +0100
csiph-web