Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a map of lambdas in C++

How can I describe a map of lambda? I want to have a map of lambda which will be called on event (just as a simple callback). The lambda type is constant.

like image 309
Ilya Shcherbak Avatar asked Apr 24 '12 08:04

Ilya Shcherbak


1 Answers

Use the <functional> header and the std::function template class. This allows you to specify function objects with a fixed method signature.

std::map< unsigned int, std::function<int(int,int)> > callbackMap;

Assuming that you index the callbacks using an unsigned int, the above map stores functions that take in two int and return an int.

like image 66
devil Avatar answered Sep 29 '22 16:09

devil