[PATCH] D106784: [ADT] function_ref captures function pointers by value

Sam McCall via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 26 15:58:55 PDT 2021


sammccall added a comment.

In D106784#2905600 <https://reviews.llvm.org/D106784#2905600>, @dblaikie wrote:

> What advantage would this narrow use case have over using a raw function pointer (a function pointer member, in the case of the "S" example, for instance)?

Typically you accept function_ref in an interface, and don't want to constrain callers to only passing things that can be expressed as function pointers.

In the case where we found the S bug, this wasn't the case (it was just a function pointer type with better syntax). But AFAIK that's just a coincidence.

In D106784#2905762 <https://reviews.llvm.org/D106784#2905762>, @chandlerc wrote:

> An alternative would be to delete this overload and insist callers pass a lambda that itself calls the desired function. This makes the user of `function_ref` more verbose, but it avoids having two dynamic dispatches (I think).

Yeah, the double indirection is a fair point, using function pointers to represent functions we know statically is wasteful.

But the ergonomics are pretty bad for functions with complicated (matching) signatures. And it will involve uglifying lots of places where there's no *correctness* problem even on buggy GCC. (If the function_ref is passed to a function and invoked synchronously then the pointer lives long enough).


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D106784/new/

https://reviews.llvm.org/D106784



More information about the llvm-commits mailing list