BOOST_FOREACH with ptr_vector of noncopyable object

482 Views Asked by At

I have the following:

class X : public boost::noncopyable
{...};

class Y 
{
   public:
      const boost::ptr_vector<X>& getXs() const;
   private:
      boost::ptr_vector<X> m_xs;
}

int main()
{
   Y y1;
   //...

   const boost::ptr_vector<X>& mx = y1.getXx();

   BOOST_FOREACH(boost::ptr_vector<X>::value_type x, mx)
   {
       // do something with x!
   }
}

It compiles but it doesn't link! It says that implicit default copy constructor for X is needed by the BOOST_FOREACH.

How can I iterate over the pointers to X only... no copy contructor, using BOOST_FOREACH.

Thanks.

1

There are 1 best solutions below

2
On

Try using references in BOOST_FOREACH(), .i.e.

BOOST_FOREACH(boost::ptr_vector<X>::value_type &x, mx)
{
    // do something with x!
}