paul424
paul424

Reputation: 153

C++ std::map Initlializator List failure

   std::map <ODParserETag ,std::function<void()>> procedure_map;

With

procedure_map = { {ODParserETag::ActorListETag, onUndefinedTag }, {ODParserETag::AdvancedWorkerETag, onUndefinedTag }, {ODParserETag::animationETag, onUndefinedTag }};

Fails with error :

/usr/include/c++/4.8/bits/stl_map.h:300:7: note:   no known conversion for argument 1 from ‘<brace-enclosed initializer list>’ to ‘std::initializer_list<std::pair<const ODParserETag, std::function<void()> > >’

and onU-T* is void ODParserXml::onUndefinedTag();

Is there any map container which would return some default preset value for every key ? ( like onUndefinedTag() in this example ) ... ?

Upvotes: 0

Views: 114

Answers (1)

Sebastian Mach
Sebastian Mach

Reputation: 39089

Member functions are not functions for you can only call them with an object.

A short route would be to use a lambda-function:

ODParserXml parser (...);
....

procedure_map[ODParserETag::ActorListETag] = [&] { parser.onUndefinedTag(); };

See also


Here's some code for tinkering (see http://ideone.com/iIEIiC):

#include <iostream>
#include <map>
#include <functional>

int main() {
    std::map<int, std::function<void()>> funs;
    funs[0] = [] { std::cout << "0\n"; };
    funs[1] = [] { std::cout << "1\n"; };

    funs[0]();
    funs[1]();
    funs[0]();
    return 0;
}

Upvotes: 2

Related Questions