如果空类的大小不能为 0,那么 std::tuple 有何魔力,使得 unique_ptr 的 sizeof 在 64 位机器中返回 8?
在 unique_ptr 中,成员定义为:
typedef std::tuple<typename _Pointer::type, _Dp> __tuple_type;
__tuple_type _M_t;
其中_Dp是删除器类。
编译器是 gcc 版本 4.7.1 (Debian 4.7.1-7)
最佳答案
原因是typename _Dp = default_delete<_Tp>
是一个空类,tuple
模板采用空基类优化。
如果您实例化 unique_ptr
使用非默认删除,您应该会看到大小增加。
关于c++ - std::unique_ptr 如何没有大小开销?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35801465/