I’m writing a simpler(faster) equivalent of std/boost::function. My chief concern is simplicity and efficiency, the platform is restricted to x86-64 linux, compiled with gcc and clang.
Under the above restriction, is it fair to assume that
- all function pointers, i.e., pointers to free function, member function (of POD, classes with virtual methods, derived classes, virtually inherited class…), functor, lambda… are all of size at most 16 bytes?
- And what is the alignment requirement?
For x86_64, you can safely assume that any pointer to function and/or member will not exceed the size of a pointer to a member function. For GCC this would be
sizeof(void*), for clang this would besizeof(void*)*2(last time I checked). The alignment requirements are 16 bytes. With GCC, you can rely on__BIGGEST_ALIGNMENT__pre-defined macro. It is absent on clang, however. The only thing that I could suggest is try not to assume, but use compile-time expression that calculates the biggest size.UPDATE:
As @David has pointed out, 8 bytes might not be enough in order to dispatch a member function invocation in case of multiple and/or virtual inheritance. So
sizeof(void*)*2applies in both cases in order to stay on a safe side.The best way to go remains compile-time expression using
sizeof. For example: