[bind] static array vs. pointer vector
Hi there, I have posted this question last Friday, already. But maybe my example was too confusing. Here is a more simple example. Basically, I don't see what the big difference is when using boost::bind with a pointer vector ( std::vector<info*> ) and a static array of info. Please consider the following code: #include <string> #include <algorithm> #include <functional> #include <vector> #include <boost/bind.hpp> using namespace std; using namespace boost; struct info { int a; string s; }; static info table[] = { { 16, "Hello" }, { 18, "Moin" } }; string& get_s( info* p ) { return p->s; } int _tmain(int argc, _TCHAR* argv[]) { { vector<info*> v; find_if( v.begin(), v.end() , bind( equal_to<string>() , bind( &get_s, _1 ) , "Moin" )); } { find_if( &table[0], &table[0] + 2 , bind( equal_to<string>() , bind( &get_s, _1 ) , "Moin" )); } } The second find_if wont complie using MSVC 7.1. The errors are: c:\boost\boost\bind.hpp(219) : error C2664: 'std::string &(info *)' : cannot convert parameter 1 from 'info' to 'info *' No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called c:\boost\boost\bind\bind_template.hpp(139) : see reference to function template instantiation 'R boost::_bi::list1<A1>::operator ()<boost::_bi::bind_t<R,F,L>::result_type,std::string&(__cdecl *)(info *),A>(boost::_bi::type<T>,F & ,A &,long)' being compiled with [ R=boost::_bi::bind_t<std::string &,std::string &(__cdecl *)(info *),boost::_bi::list1<boost::_bi::list_av_1<boost::arg<1>>::B1>>::result_type, A1=boost::_bi::list_av_1<boost::arg<1>>::B1, F=std::string &(__cdecl *)(info *), L=boost::_bi::list1<boost::_bi::list_av_1<boost::arg<1>>::B1>, A=const boost::_bi::list1<info &>, T=boost::_bi::bind_t<std::string &,std::string &(__cdecl *)(info *),boost::_bi::list1<boost::_bi::list_av_1<boost::arg<1>>::B1>>::result_type ] c:\boost\boost\bind.hpp(213) : see reference to function template instantiation 'boost::_bi::bind_t<R,F,L>::result_type boost::_bi::bind_t<R,F,L>::eval<const boost::_bi::list1<A1>>(A &)' being compiled with [ R=std::string &, F=std::string &(__cdecl *)(info *), L=boost::_bi::list1<boost::_bi::list_av_1<boost::arg<1>>::B1>, A1=info &, A=const boost::_bi::list1<info &> ] [snip] Any ideas? Christian
Christian Henning wrote:
Hi there, I have posted this question last Friday, already. But maybe my example was too confusing. Here is a more simple example. Basically, I don't see what the big difference is when using boost::bind with a pointer vector ( std::vector<info*> ) and a static array of info.
string& get_s( info* p ) { return p->s; }
Shouldn't this be: string & get_s( info & ) {...} because you have 'info' and not 'info *' in the array? -- Regards, dave
Thanks David. Must be Monday, again. ;-) Here the working code: #include <string> #include <algorithm> #include <functional> #include <vector> #include <boost/any.hpp> #include <boost/bind.hpp> using namespace std; using namespace boost; struct info { int a; string s; }; static info table[] = { { 16, "Hello" }, { 18, "Moin" } }; string& get_s( info& p ) { return p.s; } int _tmain(int argc, _TCHAR* argv[]) { { info* p = find_if( &table[0], &table[0] + 2 , bind( equal_to<string>() , bind( &get_s, _1 ) , "Hello" )); assert( p != &table[0] + 2 ); } } On 3/26/07, David Klein <dave_chp@gmx.de> wrote:
Christian Henning wrote:
Hi there, I have posted this question last Friday, already. But maybe my example was too confusing. Here is a more simple example. Basically, I don't see what the big difference is when using boost::bind with a pointer vector ( std::vector<info*> ) and a static array of info.
string& get_s( info* p ) { return p->s; }
Shouldn't this be:
string & get_s( info & ) {...}
because you have 'info' and not 'info *' in the array?
-- Regards, dave
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (2)
-
Christian Henning
-
David Klein