如何std :: unique_ptr没有大小开销? [英] How can std::unique_ptr have no size overhead?

查看:449
本文介绍了如何std :: unique_ptr没有大小开销?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果一个空类的大小不能为0,那么什么魔法在做std :: tuple,因此unique_ptr的sizeof在64位机器中返回8?

If the size of an empty class can't be 0, what magic is doing std::tuple so the sizeof of unique_ptr is returning 8 in a 64 bit machine?

在unique_ptr中,成员定义为:

In unique_ptr the member is defined as:

  typedef std::tuple<typename _Pointer::type, _Dp>  __tuple_type;                 
  __tuple_type  _M_t;

其中_Dp是删除类。

编译器是gcc版本4.7.1(Debian 4.7.1-7)

Compiler is gcc version 4.7.1 (Debian 4.7.1-7)

推荐答案

原因是 typename _Dp = default_delete< _Tp> 是一个空类, tuple 模板使用空基类优化。

The reason is that the typename _Dp = default_delete<_Tp> is an empty class and the tuple template employs empty base class optimization.

如果您使用非默认删除来实例化 unique_ptr ,那么您会看到大小增加。

If you instantiate the unique_ptr with a non-default delete, you should see the size increase.

这篇关于如何std :: unique_ptr没有大小开销?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆