Boost logo

Boost Users :

Subject: Re: [Boost-users] PolyCollection requiring copyable
From: Joaquin M López Muñoz (joaquinlopezmunoz_at_[hidden])
Date: 2017-10-04 07:09:31


El 04/10/2017 a las 4:16, Steven Watanabe via Boost-users escribió:
> AMDG
>
> On 10/03/2017 06:56 PM, d3fault via Boost-users wrote:
>> I'm impressed with PolyCollection. I use the list<interface*> strategy
>> tons in my projects, and I didn't think NOT using a pointer there was
>> even possible... but somehow you managed. It also makes sense why
>> PolyCollection is faster to execute etc, the tutorial was well
>> written.
>>
>> The only 'con' is that it requires a copy constructor. You mention
>> later that some copy traits thing can be used to move the "copy code"
>> outside of the class, but I am questioning the need for copying
>> altogether.
> PolyCollection uses contiguous storage (aka std::vector).
> This requires copying when the segment is resized.

Adding to Steven's answer, it'd have been feasible to not require move
constructibilty/assignability (which is the minimum requisite) but I
decided that the
resulting available API was not worth it. You can simulate that scenario
as follows:

 Â Â Â  #include <boost/poly_collection/base_collection.hpp>

 Â Â Â  struct not_moveable:std::logic_error
 Â Â Â  {
 Â Â Â Â Â  not_moveable():std::logic_error("type not moveable"){}
 Â Â Â  };

 Â Â Â  struct base
 Â Â Â  {
 Â Â Â Â Â  virtual ~base()=default;
 Â Â Â Â Â  base()=default;
 Â Â Â Â Â  base(base&&){throw not_moveable{};}
 Â Â Â Â Â  base& operator=(base&&){throw not_moveable{};}
 Â Â Â  };

 Â Â Â  struct derived1:base{};
 Â Â Â  struct derived2:base{};

 Â Â Â  int main()
 Â Â Â  {
 Â Â Â Â Â  boost::base_collection<base> c;

 Â Â Â Â Â  c.reserve<derived1>(100);
 Â Â Â Â Â  c.reserve<derived2>(100);
 Â Â Â Â Â  for(int i=0;i<100;++i){
 Â Â Â Â Â Â Â  c.emplace<derived1>();
 Â Â Â Â Â Â Â  c.emplace<derived2>();
 Â Â Â Â Â  }
 Â Â Â  }

This compiles and works fine; of course, if a segment reallocation is tried
(when adding the 101st element) a not_moveable exception is thrown.

Joaquín M López Muñoz


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net