[ Inserting <string, function pointer> into map ]
I'm having trouble with inserting some value_pairs into a map. Here's the basic idea.
// private
typedef Foo* (*Bar)( const std::string &x, int y );
typedef std::map<std::string, Bar> myMap;
template<class T>
Foo* DoThing( const std::string &x, int y ) {
return new T( x, y );
}
myMap m_map;
// some map insertion code
m_map.insert( myMap::value_type( "blah", &DoThing<SomeType> ));
m_map.insert( myMap::value_type( "blech", &DoThing<OtherType> ));
This would give a compiler error saying no matching function call to std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, Foo* (*)(const std::string&, int)>::pair(const char [5], <unresolved overloaded function type>)
Not sure what I'm doing wrong syntactically, or why I'm getting the unresolved overloaded function type
. My best guess is that it doesn't know that DoThing
returns a Foo*
.
Any help appreciated, thanks.
Answer 1
Comment converted to answer:
Just to be sure, DoThing<T>
is a global function and not a member of any class, right? If it's inside a class, it needs to be a static member function.
Also, if you take the address of a member function, some compilers insist on qualifying it with the class name, e.g. &MyClass::DoThing<T>
. Again, if it is a static member function, you get an ordinary function pointer. If a non-static member function, you get a pointer-to-member and the this
pointer must be supplied at the call site.
Answer 2
You forgot a ',' before the second &.
Answer 3
My guess is that it doesn't know how to convert the "blah" and "blech" to std::strings. If you construct these explicitly I think it should work.