mercredi 1 novembre 2017

Error with std::bind and templated member functions

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:

  1. A lambda expression that returns u8.
  2. Pointer to a member function.
  3. 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:

  1. Is there any way I can do this less convoluted? (e.g. init lists, std::function with member function ptrs, etc.)
  2. If this is the best way, what do I do about the templated ptrs?
  3. Would it better if I took the template params as args and used std::bind to resolve them (all params are either u8 or u8&.
  4. Any optimization suggestions?

Thanks, Zach.

Aucun commentaire:

Enregistrer un commentaire