This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
Re: Why is tuple used in unique_ptr
On 10 March 2013 23:26, Anton Daneyko wrote:
> Dear all, I am looking at unique_ptr implementation and I wonder what
> is the motivation to use tuple to hold the pointer and it's deleter,
> rather than letting them be members of the unitque_ptr on their own?
> It seems that doing std::get<0>(_M_t); or std::get<1>(_M_t); is less
> clear, than referring to member names of pointer and deleter.
The std::tuple implementation in libstdc++ takes advantage of the
Empty Base Optimization (see http://www.cantrip.org/emptyopt.html) so
using it in unique_ptr means that sizeof(std::unique_ptr<int>) ==
sizeof(int*), which would not be possible without the EBO. Instead of
reimplementing the EBO everytime it's needed, using std::tuple gives
you it for free (and also correctly handles empty but 'final'
classes.)
For standard library code, which is used by millions and must be as
low overhead as possible, the size of unique_ptr is very important,
and the slightly higher difficulty of reading the code is offset by
reusing the std::tuple code and not maintaining another EBO
implementation.