What's wrong with the initializer?

I am trying to create a `std::map` from `std:: string` to a pointer to member function type. I'm initializing the `map` with an initializer list (braced). When I compile, I get a message from the compiler: No matching constructor for initialization. Example: http://pastebin.com/y3ZvHCZ8 Any thoughts as to what I am doing wrong?
Use curly brackets.
1
2
3
4
5
6
void f() {
    static std::map<std::string, A::B> strToFunction {
        { "string1",       &A::B1 },
        { "string2",       &A::B2 }
    };
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <string>
#include <map>

struct A
{
    using mem_fun_ptr_type = int (A::*)(int,double) ;

    int foo( int a, double b ) { return a+b ; }
    int bar( int a, double b ) { return a*b ; }
};

int main()
{
    std::map< std::string, A::mem_fun_ptr_type > map { { "foo", &A::foo }, { "bar", &A::bar } } ;
}


Consider exploiting the flexibility provided by the polymorphic call wrapper: http://en.cppreference.com/w/cpp/utility/functional/function
Thanks to Both.

When I make the recommended adjustments, the compiler still rejects attempts to actually use the resultant pointer:
1
2
3
4
...
auto c = mapFromJLBorges["foo"];
someAObject.*c(1, 2.0);
...

I'm thinking pointer to member just are not for Me.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <string>
#include <map>
#include <functional>

struct A
{
    using mem_fun_ptr_type = int (A::*)(int,double) ;

    int foo( int a, double b ) { return a+b ; }
    int bar( int a, double b ) { return a*b ; }
};

int main()
{
    A a ;
    A* pa = std::addressof(a) ;

    {
        std::map< std::string, A::mem_fun_ptr_type > map { { "foo", &A::foo }, { "bar", &A::bar } } ;
        auto pmfn = map["foo"] ;
        (a.*pmfn)( 1, 2.3 ) ;
        (pa->*pmfn)( 1, 2.3 ) ;
    }

    {
        std::map< std::string, std::function< int( A&, int, double ) > > map { { "foo", &A::foo }, { "bar", &A::bar } } ;
        auto fn = map["foo"] ;
        fn( a, 1, 2.3 ) ;
    }

    {
        std::map< std::string, std::function< long( A*, int, int ) > > map { { "foo", &A::foo }, { "bar", &A::bar } } ;
        auto fn = map["bar"] ;
        fn( pa, 1, 2.3 ) ;
    }
}
Topic archived. No new replies allowed.