boost function with lambda bind

I'm trying to use a boost function object within a lambda bind. This is my test program and the commented line does not compile. What i'm trying to do is iterate over a container of test_str structs which has one data member (a string) and get that string and insert that string into another string. #include <string> #include <set> #include <vector> #include <iostream> #include "boost/lambda/lambda.hpp" #include "boost/lambda/bind.hpp" #include <algorithm> #include "boost/function.hpp" using namespace std; struct test_str { test_str(string s): str(s){} string str; }; int main(int argc, char* argv[]) { using namespace boost::lambda; boost::lambda::placeholder1_type Arg; typedef pair<set<string>::iterator,bool> (set<string>::*INSERT)(const string&); set<string> str_con; INSERT insert_func = &set<string>::insert; vector<test_str> v2; v2.push_back( test_str("one") ); v2.push_back( test_str("two") ); boost::function<string&(test_str&)> f = bind(&test_str::str, Arg); //for_each( v2.begin(), v2.end(), bind( insert_func, var(str_con), var(f))); for_each( v2.begin(), v2.end(), bind( insert_func, var(str_con), bind(&test_str::str, Arg))); return 0; } This is part of the error message i get from a vs2003.net compiler. Any help would be appreciated. c:\santosh\tools\boost_1_33_1\boost\lambda\detail\function_adaptors.hpp(250): error C2664: 'std::pair<_Ty1,_Ty2> (Arg1)' : cannot convert parameter 1 from 'boost::function<Signature>' to 'const std::basic_string<_Elem,_Traits,_Ax> ' with [ _Ty1=std::_Tree<std::_Tset_traits<std::string,std::less<std::string>,std::allocator<std::string>,false>>::iterator, _Ty2=bool, Arg1=const std::string & ] and [ Signature=std::string &(test_str &) ] and [ _Elem=char, _Traits=std::char_traits<char>, _Ax=std::allocator<char> ] __________________________________________________ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com

On 12/10/06, Santosh Joseph <santoshjoseph73@yahoo.com> wrote:
boost::function<string&(test_str&)> f = bind(&test_str::str, Arg); //for_each( v2.begin(), v2.end(), bind( insert_func, var(str_con), var(f)));
I think this is because f is a is a boost::function object. You'e trying to pass a functor object to a function that takes a std::string& argument, and that's what the error is saying that it can't coerce f to a std::string type. Instead of using a boost::function<...> object try passing it directly to the expression, like as follows. for_each( v2.begin(), v2.end(), bind( insert_func, var(str_con), bind(&test_str::str, Arg))); the boost::lambda::bind function returns a lambda functor. HTH
participants (2)
-
Haroon Khan
-
Santosh Joseph