Boost logo

Boost :

Subject: Re: [boost] [move] Library uploaded to sandbox
From: Ion Gaztañaga (igaztanaga_at_[hidden])
Date: 2009-02-19 02:18:32


Mathias Gaunard writes:

>
> Christian Holmquist wrote :
> > I think the doc_file_descriptor example should show how to create an object
> > directly in the vector<file_descriptor> using emplace functions, as
> > boost::move can't detect true rvalues as in c+0x.
> >
> > boost::interprocess::vector<file_descriptor> v;
> > v.push_back(file_descriptor("filename")); // <- Fails
>
> Why does it fail?
> I thought rvalues were properly detected.

Correction:

If you have these overloads:

push_back(const T &x);
push_back(BOOST_RV_REF(T) x);

Then 1 is chosen because derived to base conversion is stronger than
the conversion operator. But if you disable const overload with
enable_if<is_movable<T> > then you can do this:

v.push_back(file_descriptor("filename"));

but this will also work (and seems dangerous):

file_descriptor f;
v.push_back(f); //Note, no "move"

In Interprocess containers I've chosen a more verbose and limited approach:

//Catch by value works
file_descriptor temp(get_file_descriptor());
//Now explicit move
v.push_back(boost::move(temp));

You can also use a catch by value approach with this library:

push_back(movable m);

that will work with both:

v.push_back(boost::move(temp));
v.push_back(movable());

The downside is that you always create a temporary and code will not
conform to C++0x.

As you mentioned, limitations of the library are not mentioned in the
documentation and that's a big hole. A little example to play a bit:

#include <boost/move_semantics/move.hpp>
#include "movable.hpp"

//Catch by value
void function(movable m)
{}

//Catch by reference
void function_ref(const movable &m)
{}

//Catch by reference
void function_ref(BOOST_RV_REF(movable) m)
{}

int main()
{
   movable m;
   movable cm;

   //move returns rv ref
   BOOST_RV_REF(movable) r = boost::move(m);
   //Conversion operator applied
   BOOST_RV_REF(movable) q = movable();
   //Dangerous for a container if const overload is disabled
   BOOST_RV_REF(movable) s = m;
 
   //Catch by value, ok
   m = movable();

   //Does not compile
   //function(m);
   //Compiles
   function(movable());

   //Does not compile in C++03
   //function_ref(movable());
   //Compiles
   function_ref(boost::move(m));

   return 0;
}

Regards,

Ion


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk