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.