Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #121992

Re: How to transfer an allocated object into a function closure

From usenet@stegropa.de (Stefan Große Pawig)
Newsgroups comp.lang.c++
Subject Re: How to transfer an allocated object into a function closure
Date 2025-03-16 18:43 +0100
Organization Dark Sky
Message-ID <87o6y0vplb.fsf@ID-208667.user.individual.de> (permalink)
References <vr20f7$2e5bi$3@gwaiyur.mb-net.net> <vr2487$21gi7$1@dont-email.me> <vr3fvn$2lage$1@gwaiyur.mb-net.net>

Show all headers | View raw


  Hi Marcel!

Marcel Mueller <news.5.maazl@spamgourmet.org> writes:
> Am 14.03.25 um 21:40 schrieb Paavo Helde:
>> 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) ]() { /*...*/ } );
>
> I know. I tried this first, but I still get the same error:
>
>
> auto u(make_unique(child_error, g_error_free));
> gIdleAdd(new function<void()>([child_dir = move(child_dir), child_error =
> move(u)]()
> { OnDirFailed(child_dir.get(), child_error.get()); }));
>
>
> /usr/include/c++/9/bits/std_function.h:176:6: error: use of deleted function 
> ‘ReadDirectoryWorker::DirScan(gObject<_GFileEnumerator>)::<lambda()>::<lambda>(const
> ReadDirectoryWorker::DirScan(gObject<_GFileEnumerator>)::<lambda()>&)’
> ...
> src/easytag.cc:1144:87: error: use of deleted function ‘std::unique_ptr<_Tp,
> _Dp>::unique_ptr(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = _GError; _Dp =
> void (*)(_GError*)]’
>
>
> Not the lambda but std::function is the problem.

The functor that is passed to std::function has to be copy-constructible
(20.8.11.2.1/7 in C++11).

If you can use C++23, there is std::move_only_function.
<https://en.cppreference.com/w/cpp/utility/functional/move_only_function>

  Regards,
    Stefan

Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


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