Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bind function trouble

I'm using boost (signals + bind) and c++ for passing function reference. Here is the code:

#define CONNECT(FunctionPointer) \
        connect(bind(FunctionPointer, this, _1));

I use this like this:

class SomeClass {
  void test1() {}
  void test2(int someArg) {}

  SomeClass() {
     CONNECT(&SomeClass::test1);
     CONNECT(&SomeClass::test2);
  }
};

Second test function binding works (test2), because it has at least one argument. With first test I have an error:

‘void (SomeClass::*)()’ is not a class, struct, or union type

Why I don't able to pass functions without arguments?

like image 418
Max Frai Avatar asked Sep 25 '26 10:09

Max Frai


1 Answers

_1 is a placeholder argument that means "substitute with the first input argument". The method test1 does not have arguments.

Create two different macros:

#define CONNECT1(FunctionPointer) connect(bind(FunctionPointer, this, _1));
#define CONNECT0(FunctionPointer) connect(bind(FunctionPointer, this));

But remember macros are evil.

And use it like this:

class SomeClass {
  void test1() {}
  void test2(int someArg) {}

  SomeClass() {
     CONNECT1(&SomeClass::test1);
     CONNECT0(&SomeClass::test2);
  }
};
like image 147
Jorge Ferreira Avatar answered Sep 27 '26 23:09

Jorge Ferreira