A question about boost::bind function
I'm a newbie in boost library and sorry for my poor English.
I have a project now, it was created in Visual Studio.Net 2003 and the version of the boost library is 1.33.1, all thing is good. But, When I replaced the boost library with the new version 1.34.1 or 1.35.0, the project can't compiled correct. The error message is 'can't convert const sometype& to sometype&'. So, i force myself to read the source code of boost library and compare them betwwen version 1.33.1 and version 1.44.1. at last, I found the problem and written a test program. The following code is the test program:
#include "stdafx.h"
#include
BOOST_BIND( M T::*f, A1 a1 )
{
typedef typename _bi::dm_result< M T::*, A1 >::type result_type;
typedef _mfi::dm
Not sure why this was changed, though I've never used bind in this way
(specifically, to bind to a reference of a member variable).
Are you free to make changes to the code? This should work in either
version:
void AddUserAge( _sUserData& d ) { // <-- change this to a reference to the
struct
++d.age;
}
int _tmain(int argc, _TCHAR* argv[])
{
std::vector<_sUserData> vUserData;
for( int i = 0; i < 10; ++i )
vUserData.push_back( _sUserData() );
std::for_each( vUserData.begin(), vUserData.end(), boost::bind(
AddUserAge, _1 )); // <-- and remove the inner bind
return 0;
}
Or you could just do this in 1.34.0 or higher:
#include
I'm a newbie in boost library and sorry for my poor English.
I have a project now, it was created in Visual Studio.Net 2003 and the version of the boost library is 1.33.1, all thing is good. But, When I replaced the boost library with the new version 1.34.1 or 1.35.0, the project can't compiled correct. The error message is 'can't convert const sometype& to sometype&'. So, i force myself to read the source code of boost library and compare them betwwen version 1.33.1 and version 1.44.1.at last, I found the problem and written a test program. The following code is the test program:
#include "stdafx.h" #include
#include <vector> #include <algorithm> struct _sUserData { int m_nUserId; int m_nUserAge; _sUserData() : m_nUserId( rand() ) , m_nUserAge( rand()%100+1 ) { } };
void AddUserAge( int& age ) { ++age; }
int _tmain(int argc, _TCHAR* argv[]) { std::vector<_sUserData> vUserData; for( int i = 0; i < 10; ++i ) vUserData.push_back( _sUserData() ); std::for_each( vUserData.begin(), vUserData.end(), boost::bind( AddUserAge, boost::bind( &_sUserData::m_nUserAge, _1 ) ) ); return 0; } If i change the function AddUserAge( int& age ) to AddUserAge( const int& age ), all things is good. but I need change this parameter.
This isn't my desired answer, but thank you for your answer, however.
----- Original Message -----
From: Oliver Seiler
Newsgroups: gmane.comp.lib.boost.user
To: boost-users@lists.boost.org
Sent: Thursday, June 05, 2008 4:12 AM
Subject: Re: A question about boost::bind function
Not sure why this was changed, though I've never used bind in this way (specifically, to bind to a reference of a member variable).
Are you free to make changes to the code? This should work in either version:
void AddUserAge( _sUserData& d ) { // <-- change this to a reference to the struct
++d.age;
}
int _tmain(int argc, _TCHAR* argv[])
{
std::vector<_sUserData> vUserData;
for( int i = 0; i < 10; ++i )
vUserData.push_back( _sUserData() );
std::for_each( vUserData.begin(), vUserData.end(), boost::bind( AddUserAge, _1 )); // <-- and remove the inner bind
return 0;
}
Or you could just do this in 1.34.0 or higher:
#include
void AddUserAge( int& age ) { ++age; }
int _tmain(int argc, _TCHAR* argv[]) { std::vector<_sUserData> vUserData; for( int i = 0; i < 10; ++i ) vUserData.push_back( _sUserData() ); std::for_each( vUserData.begin(), vUserData.end(), boost::bind( AddUserAge, boost::bind( &_sUserData::m_nUserAge, _1 ) ) ); return 0; }
This compiles in 1.33, but doesn't actually increment the age; the inner
bind returns a temporary int by value, and the outer bind increments that.
Can you please try boost::bind
Thank you for your answer. boost::bind
void AddUserAge( int& age ) { ++age; }
int _tmain(int argc, _TCHAR* argv[]) { std::vector<_sUserData> vUserData; for( int i = 0; i < 10; ++i ) vUserData.push_back( _sUserData() ); std::for_each( vUserData.begin(), vUserData.end(), boost::bind( AddUserAge, boost::bind( &_sUserData::m_nUserAge, _1 ) ) ); return 0; }
This compiles in 1.33, but doesn't actually increment the age; the inner bind returns a temporary int by value, and the outer bind increments that.
Can you please try boost::bind
( &_sUserData::m_nUserAge, _1 ) and see if it helps?
--------------------------------------------------------------------------------
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (4)
-
Oliver Seiler
-
Peter Dimov
-
风舞影天
-
风舞影天(Rubiphisto)