 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
flyaflya Guest
|
Posted: Tue Apr 19, 2005 9:56 pm Post subject: how to "bind" string's member function? |
|
|
i use msvc6,the follow code cant be compiled:
#include "stdafx.h"
#include <iostream>
#include <string>
#include <list>
#include <algorithm>
#include <boostbind.hpp>
using namespace std;
int main(int argc, char* argv[])
{
list<string> slist;
slist.push_back("str1rn");
slist.push_back("str2rn");
slist.push_back("str3rn");
string str;
for_each(slist.begin(), slist.end(), boost::bind( &string::operator+=,
&str, _1));
return 0;
}
error message:
D:testtestCotest.cpp(27) : error C2914: 'bind' : cannot deduce template
argument as function argument is ambiguous
...................
d:boostincludeboost-1_32boostbind.hpp(1123) : see declaration
of 'bind'
Error executing xicl6.exe.
test.exe - 57 error(s), 0 warning(s)
why string's member function can't be binded, but other class designed by
myself can work in this way?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Kim, Seungtai Guest
|
Posted: Wed Apr 20, 2005 12:12 pm Post subject: Re: how to "bind" string's member function? |
|
|
"flyaflya"
| Quote: | why string's member function can't be binded, but other class
designed by
myself can work in this way?
|
Did you check it bellow?
http://www.boost.org/libs/bind/bind.html#Troubleshooting
But, keep in mind. You can not bind the pointer to member function
in the Standard Library if you want to do conforming programming.
Because there can be additional implementation defined default
parameters.
--
S Kim <stkim (AT) yujinrobot (DOT) com>
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Peter Dimov Guest
|
Posted: Wed Apr 20, 2005 3:52 pm Post subject: Re: how to "bind" string's member function? |
|
|
flyaflya wrote:
| Quote: | i use msvc6,the follow code cant be compiled:
#include "stdafx.h"
#include <iostream
#include
#include
#include
#include
using namespace std;
int main(int argc, char* argv[])
{
list
slist.push_back("str1rn");
slist.push_back("str2rn");
slist.push_back("str3rn");
string str;
for_each(slist.begin(), slist.end(), boost::bind( &string::operator+=,
&str, _1));
|
[...]
| Quote: | why string's member function can't be binded, but other class
designed by myself can work in this way?
|
The reason is that std::string defines several overloaded operator+= member
functions, one for string, one for char const* and one for char.
You can use
string& (string::*pm)( string const& ) = &string::operator+=;
and bind 'pm', or you can define a helper function
void append( string & s, string const & t ) { s += t; }
and use boost::bind( append, ref(str), _1 ).
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|