Q:

Can't compile lambda when I want to catch something outside

When I want to catch something outside the lambda, the compiling errors occurs. here is the code:
int caller(int (*callback)(void *arg), void * arg = NULL) {
  return callback(arg);
}

int main(int argc, char **argv) {

  const char *str = "world";
  caller([&](void *arg) {
      printf("hello %s\n", str);
      return 0;
  }, NULL);

  return 0;
}

But it's ok if I change the printf inside lambda like this (not using the outside variables):
printf("hello %s\n", str)

I tried to compile the source with apple g++ 4.2 and gnu g++ 4.6

A:

Lambdas can only be converted to function pointers when they are stateles, ie have no captures defined.
Have a look here and read about ClosureType::operator ret(*)(params)()
EDIT:
If it is up to you to define the callback parameter, define it using std::function, which can take any type of callable object, including lambdas. See below, it should compile fine with it (#include <functional> though).
int caller(std::function<int(void*)> callback, void * arg = NULL) {
  return callback(arg);
}

If you can't change the callback, then you should just pass str as the second parameter to the callback and print it out in lambda, as in here:
caller([](void *arg) {
      printf("hello %s\n", (const char*)arg);
      return 0;
  }, str);

(I know this actually won't compile due to const non const conversion, but you get what I mean).

