Is there a way to deliver some functions or operators as parameters?

Is it possible to bring functional programming to C ++ in any way, I want to pass some LAMBDA function or operators as a parameter to another function.

eg:

void test(DWORD foo)
{ 
  try { __asm { call foo; } }  // very weird way, don't think that could work
  catch (...) { () } 
}

or:

void test2(DWORD foo)
{ 
  someconnection.Open();
   __asm { call foo; }  // very weird way, don't think that could work
  someconnection.Close();
}

and use:

int main ()
{
  ...
  dosomething();
  ...
  void operator()(int n) // lambda expression, not sure if that correct way creating them
  {
     dosomething();
     dosomethingelse();
  }
  test ( *operator(5) ) // here is what I want
  test2 ( *operator(10) ) // here is what I want
  ...
  dosomethingelse();
  ...
}

I am using Visual Studio 2010 and not sure if I can use C ++ 0x there, but I can use boost if this can do what I want to do.

So, are there any ways to do this?

+3
source share
3 answers

You can by making a parameter, for example. testa std::tr1::function:

void test(std::tr1::function<void(DWORD)> func) {
    func(0);
}

You can call this with a function, a member function, or even a lambda:

test([](DWORD param) { return; });
+7
source

boost .

, . , , lambdas boost, . VS , - , ludicrously long .

, , . IMHO - , .

0

All Articles