abseil-cpp icon indicating copy to clipboard operation
abseil-cpp copied to clipboard

Feature Request: unique_function

Open BillBai opened this issue 4 years ago • 6 comments

In absl, we already have absl::bind_front, which can bind move only objects (std::unique_ptr ... ) as parameters, but the std::function is limited to be only copy constructable, and thus we can't cast the result of absl::bind_front(xxx, std::move(some_unique_ptr)) to it.

A move-only function (let's call it unique_function) may solve this.

Something like fu2::unique_function (https://github.com/Naios/function2 ), and chromium's OnceCallback (https://chromium.googlesource.com/chromium/src.git/+/master/docs/callback.md#oncecallback_and-repeatingcallback), would be really nice to have.

BillBai avatar Aug 11 '20 04:08 BillBai

There is a type progressing through C++ standardization (it just missed the C++20 cutoff) called any_invocable (in that it type-erases anything that matches the Invocable concept). We're reasonably likely to include something like that before long (there's one internally, we're still kicking the tires on it).

Note: unique_ptr is unique because out of all unique_ptrs anywhere in your program at any given time, the value is unique (or you have a bug). The same is not true for other move-only types: unique isn't the right prefix to represent "move only". The WG21 naming pattern is likely to become any_$FOO for concept named $FOO.

On Tue, Aug 11, 2020 at 12:08 AM Bill Bai [email protected] wrote:

In absl, we already have absl::bind_front, which can bind move only objects (std::unique_ptr ... ) as parameters, but the std::function is limited to be only copy constructable, and thus we can't cast the result of absl::bind_front(xxx, std::move(some_unique_ptr)) to it.

A move-only function (let's call it unique_function) may solve this.

Something like fu2::unique_function (https://github.com/Naios/function2 ), and chromium's OnceCallback ( https://chromium.googlesource.com/chromium/src.git/+/master/docs/callback.md#oncecallback_and-repeatingcallback), would be really nice to have.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/abseil/abseil-cpp/issues/763, or unsubscribe https://github.com/notifications/unsubscribe-auth/AC7CRX6IN63MSTWK7CAW3U3SAC74FANCNFSM4P2TLFJQ .

-- *If you get an email from me outside of the 9-5 it is not because I'm always on or expect an immediate response from you; it is because of work flexibility http://www.inc.com/john-boitnott/how-flexible-hours-can-create-a-better-work-life-balance.html . Evening and weekend emails are a sign I allocated some regular working hours for other things (such as family, gym, friends,...). And I encourage you to feel free to do the same.

tituswinters avatar Aug 11 '20 13:08 tituswinters

Awesome! I'm looking forward to it!

BillBai avatar Aug 12 '20 06:08 BillBai

@tituswinters the name any_XXX is because of the type erasure and not because of the move-only semantics, right?

romange avatar Oct 05 '20 02:10 romange

Ok, found this document. http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p1737r0.pdf

What's the point of having a committee if they come up with worst namings?

romange avatar Oct 05 '20 03:10 romange

Don't be rude - you can disagree while still being polite.

That said, I was in the room when that pattern was established - I thought it was too clever to be a good idea at first, but over time it's really stuck for me. A type-erased wrapper like this, especially one that is wrapping a C++20 concept, is literally the library representation of "any $FOO". "This function accepts any invocable." "This object wraps any contiguous range." As a naming convention, it's novel, but actually highly accurate and descriptive.

On Sun, Oct 4, 2020 at 11:05 PM Roman Gershman [email protected] wrote:

Ok, found this document. http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p1737r0.pdf

What's the point of having a committee if they come up with worst namings?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/abseil/abseil-cpp/issues/763#issuecomment-703371585, or unsubscribe https://github.com/notifications/unsubscribe-auth/AC7CRX6TJKSHD4ZZX7IOGMTSJEZXZANCNFSM4P2TLFJQ .

-- *If you get an email from me outside of the 9-5 it is not because I'm always on or expect an immediate response from you; it is because of work flexibility http://www.inc.com/john-boitnott/how-flexible-hours-can-create-a-better-work-life-balance.html . Evening and weekend emails are a sign I allocated some regular working hours for other things (such as family, gym, friends,...). And I encourage you to feel free to do the same.

tituswinters avatar Oct 05 '20 11:10 tituswinters

Sorry, did not mean to be rude. I understand how from implementors point of view any concept is dominant here. However, most folks already perceive invocable and function as type erasured creatures. So any does not bring new information here.

Anyway, I am not arguing since it has already been decided.

romange avatar Oct 05 '20 18:10 romange