Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does boost::ptr_list uses underlying void *?

Tags:

c++

boost

The boost ptr_list documentation states that the container uses an underlying std::list<void*>.

Why are they using this type instead of a more specialized std::list<T*>?

like image 903
Maël Nison Avatar asked Feb 19 '12 17:02

Maël Nison


2 Answers

It's probably to cut down on the number of template instantiations. If it uses a std::list<T*>, then every use of ptr_list<T> would also instantiate std::list<T*>. That's a lot of instantiations if you use ptr_list a lot.

like image 125
Nicol Bolas Avatar answered Sep 18 '22 18:09

Nicol Bolas


This makes it easy to share almost all the code regardless of the type(s) over which you instantiate it. Nearly all the code is in the single std::list<void *>. Each instantiation only adds code to cast between T * and void * where needed.

Of course, modern compilers/linkers can do a fair amount of this without such help, but that hasn't always been the case (and some people still use older tool chains, for various reasons).

like image 25
Jerry Coffin Avatar answered Sep 19 '22 18:09

Jerry Coffin