-1

I have a class (let's call it myclass). One of its private member variables is a std::function called myfunctor of return type bool and that takes two arguments:

bool
myfunction
    (const std::string & input, std::string & output)
{
    output = input;
}

The constructor of myclass shall receive a reference to the output std::string as its only parameter, so that the way to initialize it would be something like this:

myclass::myclass
    (std::string & s)
: myfunctor( std::bind(myfunction, std::placeholders::_1, s) )
{
    return;
}

I was hoping, however, that there's a way to directly use operator= from std::string. But I have still not found it. I've tried with many different combinations without luck:

std::bind( (std::string & (std::string::*) (std::string &)) &(s.operator=), placeholders::_1

and so on, but compiler (GCC 4.8.0) gave me errors like no matches converting to ....

djsp
  • 2,174
  • 2
  • 19
  • 40
  • 1
    Did you intend `=` to be `==`? – Bo Persson Apr 27 '13 at 21:09
  • I think you'll have to specify the overload, as there are several overloads of `std::string::operator=`. But are you aware storing a reference in `bind` may leave a dangling reference if the object passed to `myclass::myclass` has been destroyed? – dyp Apr 27 '13 at 21:14
  • @BoPersson No, I meant `=`. I want to assign, not to compare. @DyP How? I've tried several ways but the compiler still complains about unresolvable overloads. – djsp Apr 27 '13 at 21:22
  • 2
    Why not just use a lambda? It's so much simpler. – Benjamin Lindley Apr 27 '13 at 21:39

1 Answers1

1

You need to cast in order to specify the overload of std::string::operator= you want to use (there is more than one). Additionally, you need the object this member function acts upon (= the this pointer used within the member function).

Or, if you really need to return a bool, you could wrap the call in a lambda:

#include <iostream>
#include <string>
#include <functional>

int main()
{
    std::string mystring;
    std::function<bool(std::string const&)> f =
      [&mystring](std::string const& rhs)->bool { mystring = rhs; return true; };

    f("hello world");

    std::cout << mystring << std::endl;
}

Version with explicit overload resolution:

#include <iostream>
#include <string>
#include <functional>

int main()
{
    // nice C++11 syntax
    using assignment_FPT = std::string& (std::string::*)(std::string const&);
    // in case your compiler doesn't know that yet
    //typedef std::string& (std::string::*assignment_FPT)(std::string const&);

    std::string mystring;
    auto f = std::bind(
      static_cast<assignment_FPT>(&std::string::operator=),
      std::ref(mystring),  // either `ref` or a pointer (or it will be copied)
      std::placeholders::_1);

    f("hello world");

    std::cout << mystring << std::endl;
}
dyp
  • 38,334
  • 13
  • 112
  • 177
  • 1
    It keeps complaining. Remember that my `std::function` has to be `std::function`. Putting what you've posted makes GCC say `no matching function for call to ...`. - Ok, sorry, I've just realized that `std::string::operator=` does not return `bool` and it would be pretty difficult to make a workaround about that, so I should keep going on with a wrapper. – djsp Apr 27 '13 at 21:30
  • Why does your function return bool? What should be returned? `std::string::operator=` returns `*this`, that is, the string on the left side of the assignment `lhs = rhs`. – dyp Apr 27 '13 at 21:32
  • @Kalrish You can have another level of indirection (another call to `bind`) to convert `std::string` to `bool`, but what's that good for? What do you return, always `true`? – jrok Apr 27 '13 at 21:33
  • The function is meant to be some kind of translator. This would be the simplest one (no translation being done), but I could implement another that mapped, let's say, strings with boolean values (i.e. `true`, `false`, and so on). By returning `true` or `false`, the "translator" function shall tell the caller if the translation could be done or not (for example, I expect `on` or `off` and I receive `foobar`; that would return `false`). – djsp Apr 27 '13 at 21:40
  • 1
    Why modify your lambda behavior with `std::bind`, when you can just use the right lambda in the first place? `[&mystring](std::string const& rhs) -> bool { mystring = rhs; return true; };` – Benjamin Lindley Apr 27 '13 at 21:45
  • @BenjaminLindley +1 Because it evolved from the second example to the first example. I just basically changed it to that :) – dyp Apr 27 '13 at 21:47
  • Thanks, @BenjaminLindley, DyP (it does not let me to notify both of you... not sure how does it work). This worked like a charm. I knew a bit about lambdas, but not enough (I have just realized...). Thanks a lot, I'm reading about lambdas right now. – djsp Apr 27 '13 at 21:58