💡 TL;DR:How std::function works inside (type erasure, small buffer optimization) with usage examples, plus when C++23 std::move_only_function replaces std::function for move-only callables.
Last updated:2026-10-13
C++ std::function in Depth¶
While writing C++ code recently I used is_function, and took the opportunity to look at how it is implemented in the standard library, which raised some questions. We all learned about the three dots ..., but do you know what six dots ...... mean?
Likewise, given a piece of code like this:
Do you know what it means?
Let's dig into the details together!
1. Six Dots¶
Six dots are usually called two ellipsis operators. We can split the six dots apart and get the following three cases
template <typename... Args> void foo1(Args......)
template <typename... Args> void foo2(Args... ...)
template <typename... Args> void foo3(Args..., ...)
Now it is easier to read. Let's look at the code of std::is_function.
2. Implementation Details of is_function¶
The implementation of std::is_function involves a primary template and two partial specializations:
// 主模板,假设提供的类型不是函数类型
template<typename T>
struct is_function : std::false_type {};
// 偏特化,用于正常的函数类型
template<typename Ret, typename... Args>
struct is_function<Ret(Args...)> : std::true_type {};
// 偏特化,用于变参函数类型
template<typename Ret, typename... Args>
struct is_function<Ret(Args......)> : std::true_type {};
- The primary template assumes the given type is not a function type, and yields
std::false_typeas the result. - The first partial specialization covers normal function types, where
Retdenotes the return type andArgs...the parameter types. If the given type matches such a function signature,std::true_typeis the result. - The second partial specialization covers variadic function types, where the ellipsis indicates the function takes a variable number of arguments. Likewise, if the given type matches such a function signature,
std::true_typeis the result.
3. Usage Example¶
Finally, here is an example of using std::is_function:
#include <iostream>
#include <type_traits>
int func(int x, int y) { return x + y; }
int vararg_func(int x, ...) { return x; }
int main() {
std::cout << std::boolalpha;
std::cout << "Is func a function? " << std::is_function<decltype(func)>::value << std::endl;
std::cout << "Is vararg_func a function? " << std::is_function<decltype(vararg_func)>::value << std::endl;
return 0;
}
This will output:
Got it? End of this section~
C++23 std::move_only_function vs std::function¶
The type erasure you just read about has one hidden price: std::function requires the wrapped callable to be copy-constructible, because a copyable wrapper is part of its contract. Capture a std::unique_ptr in a lambda and try to store it — the compiler refuses:
auto make_sink() {
auto ptr = std::make_unique<std::vector<int>>(42);
return [ptr = std::move(ptr)] { return ptr->size(); }; // move-only closure
}
// std::function<std::size_t()> f = make_sink(); // ERROR: closure not copyable
std::move_only_function<std::size_t()> g = make_sink(); // OK since C++23
std::move_only_function (C++23, header
How to choose: keep std::function when callables are cheap to copy and legacy APIs demand it; switch to std::move_only_function when the closure owns something unique. Also remember std::function copies the closure on assignment — with heavy captures that is a real cost move_only_function avoids. In performance-sensitive slots, a plain template parameter still beats both: no erasure, no indirection, fully inlinable.
FAQ¶
What is the difference between std::function and std::move_only_function?¶
std::function requires copyable callables and copies them on assignment; std::move_only_function (C++23) accepts move-only callables and only moves. Behavior and call syntax are otherwise the same.
Why can't std::function hold a lambda capturing unique_ptr?¶
Because std::function must be able to copy the stored callable, and the closure's copy constructor is deleted once it captures a move-only type like unique_ptr. Use std::move_only_function or a template parameter instead.
Is std::move_only_function faster than std::function?¶
It can skip copy overhead and some bookkeeping, but both go through type erasure and an indirect call. If the call site knows the type, take the callable as a template parameter — that is the truly fast path.
Related Topics¶
- C++ Pure Virtual Functions and Abstract Classes | C++那些事
- C++ assert | C++那些事
- C++ Bit Fields | C++那些事
- Implementing C++ Polymorphism in C | C++那些事
- C++ const Explained: Constants, const Pointer vs Pointer to const | C++那些事
- The Complete C++ const Family | C++那些事
- C++ Dereference Operator * | C++那些事
- C++ Initialization and Assignment | C++那些事
- Ways to Return Multiple Values in C++ | C++那些事
- C++ mutable Keyword | C++那些事