How to return from callback function?

I want to return something from messagearrived and receive that value in main function. How can I achieve this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
typedef int(*messagehandler)(int);
int subscribe(messagehandler mh)
{
//do something
return 1;
}
int messagearrived(int a)
{
//do something
}
void main()
{
int a = subscribe(messagearrived);
}
Something like this?

1
2
3
4
5
6
7
8
9
# include <iostream>
using messagehandler = int(*)(int);

auto subscribe(messagehandler mh) { return mh(21); }
int messagearrived(int x) { return x * 2; }

int main() { // main returns `int'
  std::cout << subscribe(messagearrived) << '\n';
}
Live demo:
http://coliru.stacked-crooked.com/a/091205700e7499ce
Topic archived. No new replies allowed.