Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where is the constructor getting/setting the default allocator?

Tags:

c++

boost

In regards to the boost::circular_buffer class,

I'm able to instantiate a simple one as follows:

#include<boost/circular_buffer.hpp>
int main() {
  boost::circular_buffer<double> buffer;
}

The circular_buffer class is templatized with

template<typename T, typename Alloc>
class circular_buffer {
  ... 
  typedef Alloc allocator_type;
  ...
}

and I believe the constructor being called is

explicit circular_buffer(const allocator_type & = allocator_type()) noexcept;

What i don't understand is where buffer is getting its default allocator? The documentation states that, if one isn't explicitly provided, the Default Alloc object is an std::allocator<T>, but I don't see where this is being set. I'm not trying to change it, I'm just trying to understand the design of this class from a c++/software-engineering point of view.

like image 952
idWinter Avatar asked Mar 12 '15 02:03

idWinter


2 Answers

The class receives the allocator type as a template argument:

template<typename T, typename Alloc> 
class circular_buffer {

and the constructor argument just default-constructs an instance of that type.

If you use circular_buffer without specifying the Alloc template argument it uses the default specified in the base template declaration:

template <class T, class Alloc = BOOST_CB_DEFAULT_ALLOCATOR(T)>
class circular_buffer;

This is hiding in the circular_buffer_fwd.hpp header. The macro evaluates to std::allocator<T> or equivalent if the platform doesn't have that.

like image 178
sehe Avatar answered Oct 05 '22 23:10

sehe


The circular_buffer_fwd.hpp header takes care of setting the default allocator

template <class T, class Alloc = BOOST_CB_DEFAULT_ALLOCATOR(T)>
class circular_buffer;
like image 37
user657267 Avatar answered Oct 06 '22 01:10

user657267