llvm-project
b5a20db6 - [libc] Rename cpp::function to cpp::function_ref (#236108)

Commit
7 days ago
[libc] Rename cpp::function to cpp::function_ref (#236108) The class does not own the callable, so the old name (suggesting std::function) was misleading. While renaming it, I also made it behave more like the C++26 std::function_ref: - remove the default and nullptr constructors and operator bool. A std::function_ref is never empty, and nobody was using these anyway. - add a constructor for function pointers, which stores the pointer itself. Previously, `function<void()> f = &foo;` would store the address of the temporary pointer object and dangle as soon as the statement ended. - delete assignment from anything other than function_ref and function pointers, to prevent `f = [&] {...};`, which dangles for the same reason. Like std::function_ref, this also rejects lvalue callables (which would not dangle). These can still be assigned by wrapping them explicitly: `f = function_ref<void()>(callable);`. I also fixed a bug where binding a non-void callable to a function_ref with a void return type failed to compile, even though the constructor explicitly allowed it. There was no test for this class, so I'm adding one. Some differences from std::function_ref remain (e.g. no support for const and noexcept signatures, nontype_t constructors or constexpr). I'm not addressing those here. Assisted-by: Gemini
Author
Parents
Loading