I am currently writing a gameboy emulator for practicing C++. I have gotten to the part where I implement CPU instructions and decided a vector of std::function was a good choice.
Please note: u8 is an alias for uint8_t.
In my code, there is a vector of std::function<u8()> with three types of members:
- A lambda expression that returns
u8. - Pointer to a member function.
- Pointer to a templated member function.
I tried to use an initalizer list at first, but it didn't work. I later found out that is because I needed a call to std::bind(/*function ptr*/, this); on the pointers, but when calling this on the templated function pointers, I get the following error: no matching function for call to 'bind'. I would like to have an initalizer list, as right now it is a function with successive calls to emplace_back.
Here is the erroring line:
instruction_set.emplace_back(bind(&CPU::OPLoadDualRegister8<B, B>, this)); // 0x40 LD B, B
One interesting thing is that when B is replaced with a literal (e.g. 0x00) it works perfectly. B is a u8 and that is what the template accepts.
So:
- Is there any way I can do this less convoluted? (e.g. init lists, std::function with member function ptrs, etc.)
- If this is the best way, what do I do about the templated ptrs?
- Would it better if I took the template params as args and used
std::bindto resolve them (all params are eitheru8oru8&. - Any optimization suggestions?
Thanks, Zach.
Aucun commentaire:
Enregistrer un commentaire