Loading...
Loading...
C++26 introduces two new type-erased callable wrappers—std::copyable_function and std::function_ref—to address long-standing limitations of std::function and the earlier move_only_function. std::copyable_function preserves copy semantics while enforcing const-correct operator() qualifiers and requiring copyable targets, fixing std::function’s const-defect without ABI churn. std::function_ref is a lightweight, non-owning callable reference with trivial copyability and no null state, designed to avoid dangling references and reduce overhead. These additions reflect a broader trend in the C++ standard library: incremental redesign and replacement of legacy utilities to improve performance, correctness, and API clarity, guiding migration and maintenance decisions for systems and library authors.
C++26's new callable wrappers change how libraries and systems manage callable objects, affecting performance, ABI stability, and correctness. Tech professionals maintaining C++ code and libraries must plan migrations and API updates to leverage safer, more efficient function types.
Dossier last updated: 2026-05-24 08:17:46
The C++ standard library has been steadily deprecating and replacing long-standing features for the past 15 years, culminating in formal guidance to avoid earlier designs like std::function in favor of newer wrappers such as std::copyable_function landing in C++26. The article catalogs three tiers of “walk-backs”: formal deprecations removed via committee papers (e.g., std::auto_ptr, dynamic exception specifications, std::iterator), widely avoided but still standard features, and core containers that remain because ABI compatibility prevents fixes. The author cites committee papers and a Rust-vs-C++ benchmark showing large P99 latency gaps traced to problematic containers, arguing the standard’s evolution often admits past mistakes while leaving legacy baggage that affects performance and usage.
C++ standard library maintainers have been formally and informally backing away from longstanding library features for the past 15 years, culminating in a catalogue of deprecations and replacements. Notable formal removals include std::auto_ptr (deprecated in C++11, removed in C++17), legacy <functional> adapters, dynamic exception specifications replaced by noexcept, and the deprecation of std::iterator. The trend continues with newer entrants like std::copyable_function in C++26 which explicitly advises against its predecessor std::function. The article argues this pattern spans three tiers—formal deprecations, community-known anti-patterns, and ABI-locked containers that remain problematic—citing a Rust-vs-C++ benchmark that found large latency gaps traced to unrevisable C++ containers. This matters to developers, library authors, and compiler and ABI designers.
C++ standard-library evolution has quietly deprecated long-standing utilities: Sandor Dargo’s recent post on std::copyable_function highlights a refusal of std::function — introduced in C++11 — as “legacy” and discouraged for new code. The article traces fifteen years of the C++ standards committee incrementally replacing or redesigning callable wrappers and other library components rather than extending them, documenting design reversals and compatibility trade-offs. Key players include the C++ standards committee and library designers pushing newer abstractions (like copyable_function) to address performance, safety, and API clarity. This matters to systems programmers, library authors, and language toolchains because it signals migration paths, potential refactors, and long-term maintenance burdens in C++ codebases.
C++26 adds two type-erased callable wrappers: std::copyable_function and std::function_ref. std::copyable_function (P2548R6) mirrors std::move_only_function but is copyable, enforcing copy-constructible stored callables and making operator() qualifiers reflect the callable’s cv/ref/noexcept correctly—fixing std::function’s const-correctness issues without ABI breakage. It can convert to move_only_function and, like move_only_function, omits target()/target_type(). std::function_ref (P0792R14) is a lightweight, non-owning callable reference (akin to string_view) with reference semantics: no default ctor, no null checks, trivially copyable, and deleted assignments from non-function types to avoid dangling references. These additions reduce binary bloat and improve correctness and performance for callable wrappers.
C++26 adds two new type-erased callable wrappers to address gaps left by std::function and std::move_only_function. The new std::copyable_function (P2548R6) is copyable and enforces const-correct qualifiers on operator(), requiring stored callables to be copy-constructible and supporting const/noexcept/ref qualifiers—fixing std::function’s const-defect while allowing copying. It can implicitly convert to std::move_only_function. The new std::function_ref (P0792R14) is a lightweight, non-owning callable reference (like string_view for callables): trivially copyable, no default/null state, and prevents assignments that would lead to dangling references. Together they reduce binary bloat, improve const-correctness, and give developers more precise, efficient callable abstractions.