Boost logo

Boost Users :

From: Kobi Cohen-Arazi (kobi.cohenarazi_at_[hidden])
Date: 2006-04-27 11:32:17


Getting much much closer:

The following will work:
Add that function
    std::string& replace (std::string s, int pos, int n, const char* c)
    {
        return s.replace (pos, n , c);
    }

And use it instead of std::string::replace
            std::transform( array.begin(), array(), std::back_inserter(tr),
                           boost::bind(replace, boost::bind(&removeConst,
boost::bind(&Foo::get, _1)),
                                       0, 5, ""));

WORKS !!!

HOWEVER, if I'm putting & in replace's std::string argument, I'll get the
compile errors (If u need them, just let me know and I'll dump it here). so
that minor change (note the std::string&)
    std::string& replace (std::string& s, int pos, int n, const char* c)
    {
        return s.replace (pos, n , c);
    }

Will not work !
Why adding the & hurts?
Kobi.

On 4/27/06, Kobi Cohen-Arazi <kobi.cohenarazi_at_[hidden]> wrote:
>
> OK.
>
> Next stage:
> define a small helper:
> std::string removeConst (const std::string& s)
> {
> return s;
> }
>
> // define a ptr to member func std::string::replace
>
> std::string & (std::string::*rep)(std::string::size_type,
> std::string::size_type,
> const char *) = &std::string::replace;
>
>
> // try to transform from
> // but will not work. Compile fails
> std::transform( array.begin(), array(),
> std::back_inserter(tr),
> boost::bind(rep,
> boost::bind(&removeConst,
> boost::bind(&Foo::get, _1)),
> 0, 5, ""));
>
> // however that will work, so I probably missing something up there
> std::transform(array.begin(), array(), std::back_inserter(tr),
> boost::bind(&removeConst,
> boost::bind(&Foo::get, _1)));
>
> Any idea?
> Thanks,
> Kobi.
>
>
> On 4/27/06, Kobi Cohen-Arazi <kobi.cohenarazi_at_[hidden]> wrote:
> >
> > Hi Sebastian,
> >
> > Hmmm...... I think I know what the problem is ...
> > Foo::get is const
> >
> > struct Foo{
> > std::string get() const { ... }
> > };
> >
> > So it is not a good candidate to use in string::replace.
> > I wonder how I can work around it, since I want to transform a _copy_ of
> > that.
> > Is there a way to create a copy on the fly? something like:
> > ...bind(rep, std::string(bind(&Foo::get, _1)), 0, 5, ""));
> >
> > Thanks again for your insights.
> > Kobi.
> >
> >
> > On 4/27/06, Sebastian Redl <sebastian.redl_at_[hidden]> wrote:
> > >
> > > Kobi Cohen-Arazi wrote:
> > >
> > > > Hi,
> > > >
> > > > I've tried that, and I got a bunch of errors. I'm using boost::bind
> > > > for that, not boost::lambda.
> > > > Here is the error
> > > >
> > > > The following line:
> > > > std::transform(array.begin(), array.end(), std::back_inserter(tr),
> > > > bind(&std::string::replace, bind(&Foo::get, _1), 0, 5, ""));
> > > >
> > > > generates:
> > > > error: no matching function for call to 'bind(<unknown type>,
> > > > boost::_bi::bind_t<const std::string&, boost::_mfi::cmf0<const
> > > > std::string&, Foo>, boost::_bi::list1<boost::arg<1> > >, int,
> > > > uint32_t&, const char [1])'
> > >
> > > Hmmm ... the <unknown type> is suspicious. The problem might be that
> > > &std::string::replace is ambiguous, as it is overloaded. Try this:
> > >
> > > std::string & (std::string::*rep)(std::string::size_type,
> > > std::string::size_type, const char *) = &std::string::replace;
> > > std::transform(array.begin(), array.end(), std::back_inserter(tr),
> > > bind(rep, bind(&Foo::get, _1), 0, 5, ""));
> > >
> > > Sebastian Redl
> > > _______________________________________________
> > > Boost-users mailing list
> > > Boost-users_at_[hidden]
> > > http://lists.boost.org/mailman/listinfo.cgi/boost-users
> > >
> >
> >
>



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net