Follow up this question,
How can std::unique_ptr have no size overhead?
I did following code and result is somehow unexpected:
#include <memory>
#include <cstdio>
void f(void *p){
free(p);
}
int main(){
auto x1 = [](void *p){ free(p); };
auto x2 = [](void *p){ f(p); };
printf("%zu\n", sizeof(std::unique_ptr<int> )); // 8, expected
printf("%zu\n", sizeof(std::unique_ptr<int, decltype(&f)> )); // 16, expected
printf("%zu\n", sizeof(std::unique_ptr<int, decltype(x1)> )); // 8, unexpected
printf("%zu\n", sizeof(std::unique_ptr<int, decltype(x2)> )); // 8, unexpected
}
Last two types with lambda have size of 8, even they do same thing as f()
.
How is this made?