0

I am trying to figure out how to use the following constructor for boost::circular_buffer:

circular_buffer(capacity_type buffer_capacity, size_type n, const_reference item, const allocator_type& alloc = allocator_type());

I have a custom class:

template<class T> class Custom
{
  public:

    Custom() :
      time(0.0)
    {}

    double time;
    T data_;
};

With my circular buffer defined using the constructor taking in just a capacity:

boost::circular_buffer<Custom<T>> buffer(10);

I have not worked much with allocators, and I want to initialize my buffer with default/empty/zero values at construction, and it seems that the constructor mentioned above is the way to go about it using boost::circular_buffer, however I am not exactly sure how to with boost constructor. If it were an ordinary vector I assume I can do the following:

int num_elements = 10;
Custom<T> custom;
std::vector<Custom<T>> buffer(10, custom);

I could not really find any examples on this specifically, so any help or guidance is appreciated.

arias_JC
  • 549
  • 3
  • 15

1 Answers1

2

Just like your std::vector call where you do not specify an allocator for Custom and use the default, you can do the same thing with boost::circular_buffer. The only difference is boost::circular_buffer has an extra parameter allowing you set the capacity and number of default constructed objects in the buffer at the same time. That means if you want a full boost::circular_buffer then you would use:

int num_elements = 10;
Custom<T> custom;
boost::circular_buffer<Custom<T>> buffer(num_elements, num_elements, custom);

Which give you a boost::circular_buffer with a capacity of num_elements with num_elements default allocated objects in it.

You could also have:

boost::circular_buffer<Custom<T>> buffer(num_elements, num_elements/2, custom);

Which sets the capacity to num_elements but only fills half the buffer (num_elements/2) with default allocated objects.

Really the only reason you would have to use a different allocator is if you want the allocator to use a different allocation scheme (how it actually allocates memory in the system) than the default.

quamrana
  • 37,849
  • 12
  • 53
  • 71
NathanOliver
  • 171,901
  • 28
  • 288
  • 402
  • Ok this is great. Question: Now when I push back it will start filling at the end since all items are filled, and my size will return num_elements. Is there any way around this? Basically I wanted default values so whenever I accessed buffer[index] that was not populated yet but still within capacity, it would not error out. But I kind of wanted the behavior of empty buffer where size only returns items I specifically pushed back and when I explicitly pushed back it would start at the beginning (ignoring default). Not sure if thats even possible, might have to overload some operators/methods? – arias_JC Apr 21 '17 at 14:22
  • @arias_JC Just like a vector it is either one or the other. either you fill it by default and override with `operator[]` or you set it's capacity and you fill it with `push_back()`. You could wrap the buffer in your own class and provide the functionality you want if you really must have it. – NathanOliver Apr 21 '17 at 14:28