jeudi 27 avril 2017

maps of enums and lambdas in C++

currently working on a small 3rd person game, I am facing some problems working with lambdas in maps. I want to map a function to a specific enum element. To be specific: I have a keycode vector

std::vector<EKEY_CODE> _keys;

It holds elements given in an enum:

enum EKEY_CODE
{
    KEY_LBUTTON          = 0x01,  // Left mouse button
    KEY_RBUTTON          = 0x02,  // Right mouse button
// ...
}

For function calling, I have chosen to take a map:

using KeyMap = std::map<EKEY_CODE, std::function<void(const EKEY_CODE&, f32, f32)>>;

which is declared in the Game.h file:

KeyMap _keyActions;

The key vector is filled in the constructor:

Game::Game() : 
_keys({ KEY_KEY_W, KEY_KEY_A, 
KEY_KEY_S, KEY_KEY_D, KEY_KEY_C, KEY_LSHIFT, KEY_SPACE })
{
// ...
}

Now, I made a function to fill the key map:

void Game::fillMaps() {

auto move = [](const EKEY_CODE&, f32 movSpeed, f32 fdt) {
    // do funny things here
};

for (EKEY_CODE& key : _keys) {
    _keyActions.insert(key, move);
}

}

Now my problem is now that IntelliSense says "no instance of overloaded function std::map<_Kty, _Ty, _Pr, _Alloc>::insert [...] matches the argument list". As I have seen it, I have given all parameters the right way for the lambda function. Is there anything I might not have seen in this? Shouldn't the insert function of the map be able to handle the lambda function?

Thanks in advance!

Aucun commentaire:

Enregistrer un commentaire