Boost logo

Boost :

Subject: [boost] [move] Implicit conversion to rvalues and move ctors.
From: Mostafa (mostafa_working_away_at_[hidden])
Date: 2014-08-25 20:59:16


Here's another use case that fails to compile in C++03 but compiles
successfully in C++11. (Note, it fails to compile in VS2005 and g++ 4.8.2
-std=c++98, and compiles successfully with g++ 4.8.2 -std=c++11)

MOC is a BOOST_MOVABLE_BUT_NOT_COPYABLE type. Foo is an empty with an
implicit conversion function to MOC. "MOC m(Foo());" compiles successfully
but "MOC m2(makeFoo());" fails to compile. Is this a bug or a limitation
of Boost.Move in C++03?

//----------
//Start Code
//----------

#include <boost/move/core.hpp>
#include <boost/move/utility.hpp>

struct MOC
{
   MOC() : value(121) {}
   MOC(BOOST_RV_REF(MOC) rhs) : value(rhs.value) {}
   int value;
private:
   BOOST_MOVABLE_BUT_NOT_COPYABLE(MOC)
};

struct Foo
{
   operator MOC ()
   {
     return MOC();
   }
};

Foo makeFoo()
{
   return Foo();
}

int main()
{
   // (1) Successfully Compiles.
   MOC m(Foo());

   // (2) Fails to Compile.
   MOC m2(makeFoo());

   return 0;
}

//--------
//End Code
//--------

//--------------------
//Start Error Messages
//--------------------
boost_move_implicit_conversion_to_rvalue.cc: In function ‘int main()’:
boost_move_implicit_conversion_to_rvalue.cc:32:19: error: no matching
function for call to ‘MOC::MOC(Foo)’
    MOC m2(makeFoo());
                    ^
boost_move_implicit_conversion_to_rvalue.cc:32:19: note: candidates are:
boost_move_implicit_conversion_to_rvalue.cc:10:3: note: MOC::MOC(MOC&)
    BOOST_MOVABLE_BUT_NOT_COPYABLE(MOC)
    ^
boost_move_implicit_conversion_to_rvalue.cc:10:3: note: no known
conversion for argument 1 from ‘Foo’ to ‘MOC&’
boost_move_implicit_conversion_to_rvalue.cc:7:3: note:
MOC::MOC(boost::rv<MOC>&)
    MOC(BOOST_RV_REF(MOC) rhs) : value(rhs.value) {}
    ^
boost_move_implicit_conversion_to_rvalue.cc:7:3: note: no known
conversion for argument 1 from ‘Foo’ to ‘boost::rv<MOC>&’
boost_move_implicit_conversion_to_rvalue.cc:6:3: note: MOC::MOC()
    MOC() : value(121) {}
    ^
boost_move_implicit_conversion_to_rvalue.cc:6:3: note: candidate expects
0 arguments, 1 provided
//------------------
//End Error Messages
//------------------


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