C++ Logo

sg14

Advanced search

Re: [isocpp-lib] std::function

From: Arthur O'Dwyer <arthur.j.odwyer_at_[hidden]>
Date: Fri, 14 Oct 2022 10:01:01 -0400
On Fri, Oct 14, 2022 at 9:33 AM Marco Foco via Lib <lib_at_[hidden]>
wrote:

> Out of curiosity, would some of the use cases for gaming/realtime
> applications be covered by P0792 (function_ref)?
>

I don't work in gaming/realtime, but I have a strong intuition about this
specific question in general:
- `function_ref` is fantastic for places that need to take a callback or
predicate in the same way as STL algorithms, but not-be-templates (e.g.
anywhere that crosses an ABI boundary, e.g. being `virtual`, or being
implemented in a different .cpp file). It will be useful in the same ways
`string_view` is useful. It will be generally useful in "ordinary"
codebases and "SG14" codebases alike.
- (Aside: However, `std::function_ref` will likely not be acceptable to
every codebase, in the same way `std::function` isn't acceptable to every
codebase today. But that's why I teach type erasure as a technique, so that
people can go implement versions that are acceptable under their specific
business constraints
<https://quuxplusone.github.io/blog/2019/03/27/design-space-for-std-function/>.
:))
- But specifically in gaming, my impression is that a lot of what they use
type-erased `function` for is for *stored* vectors of callbacks or
handlers: "when this object is hit, do X and Y and Z." For that purpose,
you must have an *owning* type-erased thing, like `move_only_function` or
`function`, not `function_ref` — for the same reason that a vector of names
needs `std::string` and not `std::string_view`. So, *specifically* in
gaming, there is this very common use-case that is *not* solved by
`function_ref`.
- Another example of "stored vector of callables" is the queue of a task
scheduler, which is also something you might see a lot in gaming, I'd
hazard a guess.

TLDR: *some* definitely yes, *all* definitely not, and I don't think this
answer is specific to SG14.

–Arthur

Received on 2022-10-14 14:01:15