0

I have two class member functions to get bind. I have to pass placeholders, because the second class will set the arguments by themself.

Let's say I have a class

class slave
{
public:
   void func1();
   void func2(bool a, bool b);
};

and a second class

class master
{
public:
   std::function<void(void)> func1;
   std::function<void(bool, bool)> func2;
};

I can bound func1 und func2 like this

master Master;
slave Slave;

Master.func1 = std::bind(&slave::func1, &Slave);
Master.func2 = std::bind(&slave::func2, &Slave, true, false);

but how can I bind with placeholders?

without binding to a class member function I just can write:

std::function<void(bool, bool)> func2;
func2 = std::bind(&slave::func2, &Slave, std::placeholders::_1, std::placeholders::_2);

but I can't write

Master.func2 = std::bind(&slave::func2, &Slave, std::placeholders::_1, std::placeholders::_2);

How can I use placeholders for binding between class member functions?

Mo Cloun
  • 11
  • 5
  • What exactly do you mean by `"but I can't write"`? Please edit your question to include any error messages etc. – G.M. Dec 15 '19 at 11:43
  • 1
    Why can't you write it? If an error is reported you should include it *verbatim* in your post. [I cannot reproduce](https://ideone.com/AAHDSc). – WhozCraig Dec 15 '19 at 11:46
  • member functions have implicit first argument(pointer to the instance of the class, aka this), so that could be your problem. – NoSenseEtAl Dec 15 '19 at 11:48
  • https://stackoverflow.com/questions/37636373/how-stdbind-works-with-member-functions – NoSenseEtAl Dec 15 '19 at 11:51
  • I have a german Visual Studio v15: It tells: "Kein "=" -Operator stimmt mit diesem Operanden überein. Sorry, I don't know what is the english bug report. Therefore I did not included it. – Mo Cloun Dec 15 '19 at 11:51
  • I know about the mentions question mentioned above. But it uses auto which can't be used in classes. – Mo Cloun Dec 15 '19 at 11:53
  • Don't use bind, use lambdas. `Master.func2 = [&](bool a, bool b){ Slave.func2(a, b); };` – super Dec 15 '19 at 11:54
  • Thanx, Lambda is working! – Mo Cloun Dec 15 '19 at 12:05
  • I recommend sticking to naming conventions: Class names start with upper case, instance names with lower case. You did it the other way around, which makes the code a bit harder to read. And while you're at it, maybe [drop the problematic terminology](https://www.theserverside.com/opinion/Master-slave-terminology-alternatives-you-can-use-right-now) as well. – Max Vollmer Dec 15 '19 at 12:05
  • @MaxVollmer: Yes, you are right. Thank you for your comment! I was never thinking about the second hint. – Mo Cloun Dec 15 '19 at 12:25
  • The error code ("Cxxxx" or "LNKxxxx") are not translated and they are very useful search keys online. – Ulrich Eckhardt Dec 15 '19 at 12:25
  • The error message means "no operator= matching ..." ... so the return value of that bind call seems to have a different type than `master.func2` ... – Daniel Jour Dec 15 '19 at 14:03

0 Answers0