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 ) ;
}
}
| |