Boost.Bind to access std::map elements in std::for_each

后端 未结 4 871
后悔当初
后悔当初 2020-12-16 03:49

I\'ve got a map that stores a simple struct with a key. The struct has two member functions, one is const the other not. I\'ve managed calling the const function using std::

4条回答
  •  无人及你
    2020-12-16 04:36

    IIRC, Boost.Bind uses boost::mem_fn for its binding to members capability. Now, if you look at mem_fun (scroll down to the // data member support part), you'll see that it typedefs its result_type as a const&, while is still has overloads of the function call operator supporting the extraction of a non-const member from a non-const argument.

    It thus seems that the problem is that this confuses Boost.Bind's return type deduction mechanism. A solution would thus to explicitly tell Bind that the result is not const:

    //call the non-const member function
    std::for_each(theMap.begin(), theMap.end(),
       boost::bind(&MyStruct::someFunction, 
           boost::bind(&MyMap::value_type::second, _1)
       )
    );
    

提交回复
热议问题