ARTICLE AD BOX
There is no max size. C++ standard does not mandate that a C++ implementation be non-hostile, remember!
There is a min size implied by the implicit requirement that std::function be able to store a function pointer without allocating. For most hardware, this gives you a minimum sizeof(void(*)()), plus enough information to convert the arguments (as the function pointer need not match!).
(std::function may not throw when storing a function pointer or a reference wrapper, which in turn implies it cannot allocate memory, which means it needs to use the small buffer optimization in that case).
A reasonably efficient way to handle this problem is to make your function look like this:
template<...> struct function { ftable* vtable; std::byte buffer[SOME_SIZE]; };then store the operations on the buffer in the vtable, like:
struct ftable { void(*destroy)(void*); void(*copy)(void*, void const*); void(*invoke)(void*, Args&&...); // etc for move/get typeid/etc. };Now, the vtable need not be a full sized pointer; it just has to name a global table of the proper type. The compiler could assume or prove that there are fewer than 2^16 type erasures required and store an index into that table instead, reducing the size of the variable vtable to 16 bits. Then stick it after the buffer to allow for better alignment.
But using a full on pointer makes it easier.
When using a non-SBO type, the vtable functions are replaced with ones that know the void* points at the pointer instead of at the actual object, and we store the pointer in buffer.
To store the largest of a member function pointer, reference wrapper and a function pointer, on modern platforms I am familiar with the buffer needs to be 16 bytes in size, plus 8 for the vtable pointer.
The "index to say how to deal with the data", plus a member function pointer (including to a class with virtual members; msvc makes them larger), is the practical lower bound.
As a first aside, some compilers only use the SBO for exactly the types mandated. Others take any "small enough" object and stuff it in the SBO to reduce allocations (at the cost of move being more expensive possibly).
As a second aside, your chatbot spewed half-raw nonsense at you. Please disregard anything it told you; it contains a mixture of half correct and blatantly wrong statements that literally poison your understanding, as the wrong information is presented just as confidently as the vaguely right stuff.
