0

The pseudo code for what I want to do is:

function<bool(int)> getFunc(type) // get a function depending on what type is passed

problem is the function to return must be declared as static? As a result, I can't access object properties. So I need to pass them into the function? Thus, the original function to return might look like:

bool func1(int)
bool func2(int)

Now needs to be injected with other objects/arguments it need to run ...

bool func1(int, Class1)
bool func2(int, Class2)

So how do I define the return type of getFunc? Or maybe theres a better way?

UPDATE

In the above, func* functions are actually: has*(). eg.

hasStmtsUsing(variable)
hasVariablesUsed(stmt)

And to determine if the condition is true, it uses an object eg. uses. Then there are other similar has*() functions like hasStmtsModifying(variable) that uses an object modifies. uses and modifies are objects of different types, and originally, they are object members, thus dont need to be passed in. Now since the functions are static, they need to be passed in.

While writing this, I am thinking what I need is some kind of dependency injector? Maybe I pass in DI and call DI.getX() functions?

Jiew Meng
  • 84,767
  • 185
  • 495
  • 805
  • 1
    Passing variadic functions around sounds like asking for trouble. Can you explain why you're doing this? (also it may not be possible, because to my knowledge variadic functions are handled by the compiler in a special way) – Dave Mar 30 '13 at 04:40
  • See here: http://stackoverflow.com/questions/4972157/is-a-pointer-to-a-function-which-have-unknown-number-of-parameters-possible – Dave Mar 30 '13 at 04:40
  • @Dave, I updated my post btw – Jiew Meng Mar 30 '13 at 06:18

2 Answers2

1

Maybe I'm misunderstanding something, but isn't all you need to use a memberfunction where you bind() the first parameter?

class X {
    bool f1(int);
    bool f2(int);
};

X x;
function<bool(int)> f = bind(&X::f1, &x);
Ulrich Eckhardt
  • 16,572
  • 3
  • 28
  • 55
  • Hmm just looked at the docs for bind, ... but I dont exactly understand what it does. But looking at your example, can I say the result when I call `f` is as if `x.f1` is called? So I can get object properties of `x`? – Jiew Meng Mar 30 '13 at 06:17
  • I tried `bind(&QueryEvaluatorPrivate::hasVarsUsed, this)` but VS complains of `C2825: '_Fty': must be a class or namespace when followed by '::'`. – Jiew Meng Mar 30 '13 at 06:29
  • I've edited my function to look like: http://pastie.org/7169474 it works by it seems like quite abit of duplicate code the lambda syntax and the return stuff ... interested to know whats wrong with the bind too. Is my previous comments about bind correct? It does not seem to work with `this` – Jiew Meng Mar 30 '13 at 07:36
  • You guessed right, f(42) should call x.f1(42). The function X::f1 takes two parameters, the implicit "this" and the integer. The bind call sets the first parameter to &x, at least had gotten the syntax right: http://stackoverflow.com/questions/5154116/stdfunction-to-member-function. Hope that helps. – Ulrich Eckhardt Mar 30 '13 at 13:53
0

Here's an example of how it can be done with lambdas in C++11:

#include <cassert>
#include <functional>
#include <iostream>

struct Class1 {
};

struct Class2 {
};

bool func1(int,Class1)
{
  return true;
}

bool func2(int,Class2)
{
  return false;
}

inline std::function<bool(int)> getFunc(Class1 obj1)
{
  return [=](int x){ return func1(x,obj1); };
}

inline std::function<bool(int)> getFunc(Class2 obj2)
{
  return [=](int x){ return func2(x,obj2); };
}

int main(int,char**)
{
  Class1 obj1;
  std::function<bool(int)> f1 = getFunc(obj1);
  Class2 obj2;
  std::function<bool(int)> f2 = getFunc(obj2);
  assert(f1(0)==true);
  assert(f2(0)==false);
  return 0;
}
Vaughn Cato
  • 63,448
  • 5
  • 82
  • 132