ref: https://www.zhihu.com/question/531686307/answer/2473883018
#include <cstdio>
#include <memory>
using namespace std;
struct file_closer {
void operator()(FILE* fp) const
{
fclose(fp);
}
};
void test1(const char* filename)
{
unique_ptr<FILE, file_closer> ptr{fopen(filename, "r")};
printf("sizeof of unique_ptr<FILE, file_closer> is %zu\n", sizeof ptr);
}
void test2(const char* filename)
{
unique_ptr<FILE, void (*)(FILE*)> ptr{fopen(filename, "r"),
[](FILE* fp) { fclose(fp); }};
printf("sizeof of unique_ptr<FILE, void (*)(FILE*)> is %zu\n", sizeof ptr);
}
#if __cplusplus >= 202002L
void test3(const char* filename)
{
unique_ptr<FILE, decltype([](FILE* fp) { fclose(fp); })> ptr{
fopen(filename, "r")};
printf("sizeof of unique_ptr<FILE, decltype(lambda)> is %zu\n", sizeof ptr);
}
#endif
int main()
{
test1("test.cpp");//8
test2("test.cpp");//16
#if __cplusplus >= 202002L
test3("test.cpp");//8
#endif
}