当我们添加任何数据类型时,std::unique_ptr 占用太多 space

std::unique_ptr takes too much space when we add any data type

#include <iostream>
#include <memory>

int main() {
    int i = 0;
    float f = 0.0f;
    double d1 = 0.0, d2 = 0.0, d3 = 0.0, d4 = 0.0;

    auto a = [i,f,d1,d2,d3,d4](){};

    std::cout << sizeof(std::unique_ptr<decltype(a)>) << std::endl;       //  8
    std::cout << sizeof(std::unique_ptr<char, decltype(a)>) << std::endl; // 48

    return 0;
}

为什么我只添加一个字符时这个程序的输出是 48?

Why the output of this program is 48 when I add just one char?

因为您指定了一个删除器,其中包含一个 int 一个 float 和 4 个双精度数。该删除器存储为唯一指针的子对象。

请注意,由于删除器不满足必要的要求,因此这种唯一指针首先不起作用。特别是,它不接受传递给它的参数。