Search code examples
c++booststlboost-bind

boost::bind with maps, what's the difference between binding std::pair and std::map::value_type?


What's the difference between the following two cases?

std::pair<int,std::string> example_1 (std::make_pair (1,"foo"));
int value_1 = boost::bind (&std::pair<int,std::string>::first,_1) (example_1);

std::map<int,std::string>::value_type example_2 (std::make_pair (2,"boo"));
int value_2 = boost::bind (&std::pair<int,std::string>::first,_1) (example_2);

The first example works fine but the second does not compile when the binding is done. I have looked at the file stl_map.h and value_type is defined as follows:

typedef std::pair<const _Key, _Tp> value_type;

I don't see the difference. I would appreciate is someone can tell let me know and the reason why the second example does not compile.

The compilation error message is:

.../include/boost/bind/mem_fn.hpp:333:36: error: no matching function for call to ‘get_pointer(const std::pair<const int, std::basic_string<char> >&)’
make: *** [main.o] Error 1

Thanks in advance!


Solution

  • map's value_type has a const key (const int in your case), whereas the pair you're using doesn't (plain int in your case).