Hash table of function pointers?

Is it possible to create a hash table of function pointers so that functions can be referenced by their name (using a string?)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <functional>
#include <unordered_map>

int foo( double ) { std::cout << "foo was called\n" ; return 0 ; }
long bar( float ) { std::cout << "bar was called\n" ; return 1 ; }

// http://en.cppreference.com/w/cpp/container/unordered_map
// http://en.cppreference.com/w/cpp/utility/functional/function
std::unordered_map< std::string, std::function< long(double) > > hash_table = { { "foo", foo }, { "bar", bar } } ;

int main()
{
    hash_table["bar"]( 23.456 ) ;
}

http://coliru.stacked-crooked.com/a/14fa1fb5721af85f
Thanks! Appreciate it.
Topic archived. No new replies allowed.