std::function initialization inside class

Viewed 1790

I am trying to understand how std::function works and I am not able to compile this and I don't understand why. I think it has something to do with using std::function inside class because without classes (map defined in global scope) it worked.

This is the error message I get:

functor.cc:37:9: error: could not convert ‘{{"A", ((C*)this)->C::f}, {"B", ((C*)this)->C::g}, {"C", ((C*)this)->C::h}}’ from ‘<brace-enclosed initializer list>’ to ‘std::map<std::__cxx11::basic_string<char>, std::function<bool(const std::vector<std::__cxx11::basic_string<char> >&)> >’

Sample code (it makes no sense but it pretty represents the problem I have):

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


class C { 
    public:
        bool f(const std::vector<std::string>& s) {
            std::cout << "F" << std::endl;
            for (auto& i : s) {
                std::cout << i << std::endl;
            }
            return true;
        }

        bool g(const std::vector<std::string>& s) {
            std::cout << "G" << std::endl;
            for (auto& i : s) {
                std::cout << i << std::endl;
            }
            return true;
        }

        bool h(const std::vector<std::string>& s) {
            std::cout << "H" << std::endl;
            for (auto& i : s) {
                std::cout << i << std::endl;
            }
            return true;
        }

        std::map<std::string, std::function<bool(const std::vector<std::string>&)> >  funcMap {
            {"A", f},
            {"B", g},
            {"C", h}
        };
};


int main() {
    std::vector<std::string> v{"mno", "pqr", "stu"};
    C c;
    c.funcMap["A"](v);
}
1 Answers
Related