skip to content

Designing a C++ job scheduler library used by many teams, how do you choose between a template parameter, std::function, std::move_only_function and std::function_ref for callbacks?

level: principalimportance: should knowfreq 40%

answer

  1. called now, or stored for later?
  2. who owns the callable
  3. header-only templates versus a stable ABI
  4. which standard every team builds with

basics

~20 s

Choose by lifetime and ownership: a template parameter or C++26 std::function_ref for callbacks run before the call returns; C++23 std::move_only_function for stored, single-owner jobs; std::function only where callers genuinely need to copy a stored callback.

solid answer

~40 s

Start from **lifetime**. A callback used only during the call, such as a `for_each_job` visitor, needs no ownership: a constrained **template parameter** is zero-overhead, and **C++26** `std::function_ref` gives a small, non-owning, non-template parameter where code must stay out of headers. A callback the scheduler **stores** must be owned: **C++23** `std::move_only_function` fits most job queues, accepts move-only state, and carries `const` and `noexcept` in its signature; `std::function` remains for callbacks the scheduler must copy or for teams stuck before C++23. Then weigh **compile time and ABI** (templates live in headers and instantiate per caller; a wrapper can sit behind a compiled interface), **per-call cost** (indirect call, possible allocation), and **toolchain support**, since libc++ in LLVM 23 ships neither `move_only_function` nor `function_ref`. The judgment is setting one rule per callback role and documenting it.

code

cpp · 31 lines
cpp
#include <concepts>
#include <cstdio>
#include <functional>
#include <utility>
#include <vector>

struct Job { int id; };

class Scheduler {
  std::vector<Job> jobs_{{1}, {2}, {3}};
  std::vector<std::function<void()>> on_idle_;  // stored: must own its callables
 public:
  // Called only during this call: a constrained template, no type erasure
  template <std::invocable<Job&> F>
  void for_each_job(F&& visit) {
    for (Job& j : jobs_) visit(j);
  }
  // Kept for later: an owning, type-erased wrapper
  void on_idle(std::function<void()> cb) { on_idle_.push_back(std::move(cb)); }
  void idle() {
    for (auto& cb : on_idle_) cb();
  }
};

int main() {
  Scheduler s;
  int total = 0;
  s.for_each_job([&total](Job& j) { total += j.id; });  // safe: runs before return
  s.on_idle([total] { std::printf("idle after %d\n", total); });  // copies total
  s.idle();
}

go deeper

for a junior

Know that a callback can be taken as a template parameter or as std::function, and that std::function can store it for later.

for a middle

Explain the difference between owning and non-owning callback types and why a stored callback must own its state.

for a senior

Measure the per-call and per-submit cost of each option and catch stored non-owning references in review.

for a principal

Own the rule per callback role, balancing ABI stability, compile time, toolchain floors across teams and per-call cost, and plan the fallback for C++26 types.

## The decision a lead owns A scheduler used by many teams will be called with every kind of callable: lambdas with large captures, member functions, move-only state. The callback parameter type is part of the library's API and often its ABI, so changing it later breaks every caller. There is no single right answer; there is a right answer **per callback role**, and the lead's job is to fix those roles and their types. ## The candidates | Parameter type | Owns the callable? | Stored? | Per-call cost | Standard | |---|---|---|---|---| | constrained template `F&&` | caller's choice | only by the template's own code | direct, inlinable | C++20 concepts, or C++17 traits | | `std::function_ref<Sig>` | no | must not outlive the call | one indirect call, no allocation | C++26 | | `std::move_only_function<Sig>` | yes, single owner | yes | indirect call, may allocate | C++23 | | `std::function<Sig>` | yes, copyable | yes | indirect call, may allocate | C++11 | | `std::copyable_function<Sig>` | yes, copyable | yes | indirect call, may allocate | C++26 | | function pointer plus `void*` context | no | caller manages lifetime | one indirect call | C, stable across ABIs | The C++26 draft describes `std::function_ref` as storing a pointer to a thunk function and a trivially copyable **bound entity** able to hold an object or function pointer, so it is typically two pointers wide; it has no empty state, and copying it never touches the target. ## Matching type to role 1. **Called during the call** (`for_each_job(visit)`, a filter predicate): the template parameter when the function can live in a header, `std::function_ref` when it must be compiled once behind an interface. Capturing by reference in the caller's lambda is then safe, because the call ends before the caller's locals do. 2. **Stored and run later** (`submit(job)`, `on_idle(cb)`): an owning wrapper. `std::move_only_function<void()>` is the natural default from C++23; it accepts a job that owns a `std::unique_ptr` and avoids paying for a copy constructor nobody uses. 3. **Stored and copied** (one callback fanned out to several queues): `std::function`, or `std::copyable_function` in C++26, which fixes `std::function`'s `const` call operator that can still mutate its target. 4. **Crossing a C ABI or plugin boundary**: a function pointer with a `void*` context, wrapped on each side. 5. **Hot path with measured allocation cost**: a project-owned fixed-capacity callable, or a template, as the profiling question describes. ```cpp // C++26, not compiled here: libc++ in LLVM 23 has no std::function_ref void for_each_job(std::function_ref<void(Job&)> visit); // called before return void submit(std::move_only_function<void()> job); // stored, C++23 ``` ## The trade-offs behind the table - **Compile time and code size**: templates instantiate per callable type in every caller and expose the implementation in headers; wrappers compile once. - **ABI stability**: a wrapper type in a shared library's interface ties you to that standard library's layout; a template parameter never crosses the boundary. - **Diagnostics**: a constrained template, for instance with `std::invocable<Job&>`, rejects a wrong callback at the call site with a readable message. - **Toolchain floor**: every team must build with the chosen standard. libstdc++ in GCC 16 ships `std::move_only_function`, `std::copyable_function` and `std::function_ref`; libc++ in LLVM 23 ships none of them, so a portable library may need a fallback, such as `std::function` or a small in-house non-owning callable, behind a type alias. - **Misuse risk**: `std::function_ref` dangles if stored; document it, and review any member of that type. ## Making the rule stick - Publish the roles and their types in the library's API guide, with one example per role. - Give each role a library-owned alias, such as a `visitor` and a `job` type, so a later switch from `std::function` to `std::move_only_function` or `std::function_ref` is one edit. - Flag in review any data member whose type is a non-owning callable. - Re-measure submit cost when the alias changes, on every standard library the teams use. ## Mistakes interviewers listen for - One type for every role, usually `std::function` everywhere, paying for copyability and possible allocation on paths that need neither. - `std::function_ref` or a raw reference stored for later. - Templates everywhere in a library that promised a stable binary interface. - Choosing C++26 types without checking that every consuming team's toolchain has them.

  • Why is it safe for a for_each_job visitor to capture locals by reference, but not a submitted job?
    `for_each_job` calls the visitor and returns before the caller continues, so the caller's locals are alive for every call. A submitted job is stored and run later, possibly on another thread, after the caller's scope may have ended, so references captured into it can dangle. That lifetime difference is exactly what separates a non-owning parameter type from an owning one.
  • How do you offer C++26 function_ref in a library that must also build with libc++ today?
    Hide the choice behind a library-owned alias or class: use `std::function_ref` when the feature-test macro `__cpp_lib_function_ref` is defined, and otherwise a small in-house non-owning callable with the same shape, a context pointer and a thunk. Callers write the alias, and the switch is invisible to them once every toolchain catches up.

  • std::function is the right parameter type for every callback.
  • std::function_ref can be stored in a queue like any other wrapper.
  • A template parameter costs nothing in compile time or binary size.
  • std::move_only_function is available on every C++23 toolchain today.
  • std::move_only_function never allocates, so it suits any hot path.

Assumes C++23: std::move_only_function is C++23; std::function_ref and std::copyable_function are C++26, shipped by libstdc++ in GCC 16 but not by libc++ in LLVM 23.