Skip to content

Refactor C-style function types to std::function for improved type safety and readability #58034

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
edilson258 opened this issue Apr 26, 2025 · 2 comments

Comments

@edilson258
Copy link

Description:

In several places within the codebase, C-style function pointer definitions are still being used. While these work, they do not provide the same level of type safety, and readability as C++'s std::function. Refactoring these instances to use std::function will:

  1. Improve Type Safety: std::function provides better type checking at compile-time, reducing the chance of mismatched function signatures.
  2. Increase Readability: std::function allows for clearer, more expressive code, particularly when dealing with complex function signatures.
  3. Enhance Flexibility: std::function can easily bind to lambdas, member functions, and function pointers, providing a more flexible interface compared to raw function pointers.

Tasks:

  • Identify and locate instances of C-style function pointer definitions.
  • Refactor to use std::function with appropriate type signatures.
  • Ensure that all relevant code maintains functionality after the refactor.

Example:

If the current code uses a C-style function pointer like:

void (*callback)(int);

It could be refactored to this

std::function<void(int)> callback;
@edilson258 edilson258 changed the title Refactor C-style function types to std::function for improved type safety Refactor C-style function types to std::function for improved type safety and readability Apr 26, 2025
@bnoordhuis
Copy link
Member

(1) and (2) are debatable, (3) is mostly irrelevant - libraries like libuv and openssl don't take fat pointers, you can't pass closures to them.

@dario-piotrowicz
Copy link
Member

@edilson258 I noticed that you also posted this on the discord server (discord message)

@RafaelGSS replied with:

I wouldn’t do it until we find some clear benefits

std::function is a heavy C++ abstraction. It generates more template instantiations (more symbols, larger binary), whereas function pointers are minimal.

Node.js sometimes interacts closely with pure C libraries (e.g., OpenSSL, uv, c-ares). std::function cannot be passed to C APIs without special wrapping, while raw pointers are compatible.

(which is in line with @bnoordhuis' comment above)

and you agreed with them.

So I would imagine that we can just close this issue since everyone seems to be in agreement that the current definitions are ok, do you agree? 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants