Boost logo

Boost Users :

Subject: Re: [Boost-users] [Container] Forward declaration of movable types
From: Ion Gaztañaga (igaztanaga_at_[hidden])
Date: 2011-08-16 12:18:43


El 16/08/2011 15:00, Szymon Gatner escribió:
> My problem:
>
> If there is a movable class 'MyMovable' (I use boost.move) it can be
> stored without unnecessary copies in move-aware vector that is
> declared like so:
>
> vector<MyMovable> movables;
>
> which is very cool until this vector is suppose be a member of other class like:
>
> //widget.hpp
>
> class Widget
> {
> vector<MyMovable> movables_;
> };

You always need the include, just like you needed it when MyMovable was
copyable, move is an optimization it does not offer type erasure. If you
need to break dependencies, use pimpl idiom for all your members
(Warning: I haven't compiled it):

//hpp

//Note: no includes needed here for vector or MyMovable

class Widget
{

   Widget();
   ~Widget();

   private:
   //No explicit member declaration
   //shadow it behind Internals
   struct Internals;
   Internals *impl_;
};

//cpp
#include<vector>
#include"MyMovable.h"

struct Widget::Internals
{
    vector<MyMovable> movables_;
    vector<MyMovable> movables2_;
    vector<MyMovable> movables3_;
};

Widget::Widget()
   //Single allocation for all internal types
   : impl_(new Internals)
{}

Widget::~Widget()
{
   delete impl_;
}

Best,

Ion


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