
Hi Folks,
Can anyone tell me why this is wrong? My intention is to call A::f() for
each A constructed
on-the-fly from the elements of v.
Thanks, Rob.
#include

Hi!
This one works...
#include
Hi Folks,
Can anyone tell me why this is wrong? My intention is to call A::f() for each A constructed on-the-fly from the elements of v.
Thanks, Rob.
#include
#include #include #include <algorithm> #include <vector> struct A { A( int ); void f( ); };
int main( ) { using namespace boost::lambda;
std::vector< int > v; std::for_each( v.begin( ), v.end( ), bind( & A::f, bind( constructor<A>(), _1 ) ) ); }
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

On Tue, Mar 23, 2010 at 6:02 PM, Ovanes Markarian
Hi!
This one works...
#include
#include #include #include <algorithm> #include <vector> struct A { A(int) {} void f()const {} };
int main( ) { using namespace boost::lambda;
std::vector< int > v; std::for_each( v.begin( ), v.end(), bind(&A::f, bind<A>(constructor<A>(), _1))); }
It does indeed, making method f() const fixes it, but I still can't quite
see why. Without the
constness of f() the guts of the error says this,
/usr/include/boost/lambda/detail/actions.hpp:87: error: no matching function
for call to ‘boost::lambda::function_adaptor

On Wed, Mar 24, 2010 at 9:38 AM, Robert Jones
It does indeed, making method f() const fixes it, but I still can't quite see why. Without the constness of f() the guts of the error says this,
/usr/include/boost/lambda/detail/actions.hpp:87: error: no matching function for call to ‘boost::lambda::function_adaptor
::apply(void (A::* const&)(), const A&)’ from which it seems that the constructed required signature includes a const A&. Why is this?
Because A() is an rvalue, a temporary which cannot be assigned to a non-const reference. What you propose is pretty equal to the following ill-formed construct: void do_smth(A& a) { // do smth. } do_smth(A(7)); So the best boost::bind overload which compiler finds is that which accepts A const& => only const members are allowed to be accessed. Regards, Ovanes

On Wed, Mar 24, 2010 at 9:19 AM, Ovanes Markarian
void do_smth(A& a) { // do smth. }
do_smth(A(7));
So the best boost::bind overload which compiler finds is that which accepts A const& => only const members are allowed to be accessed.
Yes, I constructed an equivalent example, and then couldn't see the wood for the trees! Ok, I know I'm having an aberrant moment here, but remind me why a temporary can't be passed as a non-const reference. - Rob.

On Wed, Mar 24, 2010 at 11:29 AM, Robert Jones
On Wed, Mar 24, 2010 at 9:19 AM, Ovanes Markarian
wrote:
Ok, I know I'm having an aberrant moment here, but remind me why a temporary can't be passed as a non-const reference.
Because ISO C++ Standard 2003 prohibits it and explicitly states that the
lifetime of a temporary can be extended when it is bound to a
const-reference. I don't know the exact paragraph, nor have the access to
the Standard right now, but I clearly remember having read it. There is even
an example how long the temporary lives when bound to the const-reference.
The situation will change with the moving semantics and rvalue references in
the upcoming standard C++0x. If you really need to modify the internal
object state you could introduce mutable data members. Those can be changed
from const-qualified member functions. But think twice if you really need
it...
Getting back to the previous example the code looks as:
#include

Robert Jones wrote:
Hi Folks,
Can anyone tell me why this is wrong? My intention is to call A::f() for each A constructed on-the-fly from the elements of v.
Thanks, Rob.
Hi Rob -
Since I'm promoting phoenix these days (o;
Here is one way you could do it --------------------
#include <iostream>
#include <vector>
#include
participants (3)
-
Michael Caisse
-
Ovanes Markarian
-
Robert Jones