简体   繁体   中英

C++11: Table of Function Pointers, Lambdas

I'm currently working on a simple and abstract emulator for processors, and I'm using a table to translate machine-code opcodes into actions. Currently, this table holds pointers to delegate functions (provided by an external library), which are the only option I had at the time (before C++11) to make it possible to call member functions of subclass instances.

So far, Lambdas look like they can provide a perfect replacement, but I still want to support vanilla function pointers. This tutorial on lambdas shows an example where you can treat lambdas and function pointers identically, but I'm wondering if it would be possible to store a mixed array of function pointers and lambdas so I can also store them in the same place.

std::function is a mechanism to do type erasure for all things callable.

You can have a collection of std::function and put in function pointers with that signature next to lambdas with that signature. There is a bit of overhead involved compared to using function pointers directly. It's interesting to note that the type of a lambda not specified by the spec and therefore cannot be written down directly, but it can always be put in a std::function wrapper.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM