Now I am try to use boost bind & mem_fn
.
But there's a problem to bind overloaded-function.
How to resolve compile error of follow codes?
boost::function< void( IF_MAP::iterator ) > bmf = std::mem_fun1< void, IF_MAP, IF_MAP::iterator >( &IF_MAP::erase );
boost::function< void( IF_MAP::iterator ) > bmf = boost::mem_fn< void, IF_MAP, IF_MAP::iterator >( &IF_MAP::erase );
The main purpose is to compile follow codes
IF_MAP M;
boost::function< void( IF_MAP::iterator ) > bmf = boost::bind(
boost::mem_fn< void, IF_MAP, IF_MAP::iterator >( &IF_MAP::erase ),
&M, _1 );
M.insert( IF_MAP::value_type( 1, 1.f ) ); M.insert( IF_MAP::value_type( 2, 2.f ) );
bmf( 2 );
The compile error messages are like this...
error C2665: 'boost::mem_fn' : none of the 2 overloads could convert all the argument types could be 'boost::_mfi::mf1 boost::mem_fnTraits>::iterator>(R (_thiscall std::map<_Kty,_Ty>::* )(A1))' or 'boost::_mfi::cmf1 boost::mem_fnTraits>::iterator>(R (_thiscall std::map<_Kty,_Ty>::* )(A1) const)'
P.S. As U know, std::map has 3 overloaded erase member function
void erase(iterator _Where)
size_type erase(const key_type& _Keyval)
void erase(iterator _First, iterator _Last)
2nd function can be binded easily, but others not.
Edit
To describe my question in more detail:
Actually, I want to make deferred function call. When I meet return code of function, then it's time to scope out, so deferred function should be called.
Now I am refactoring some legacy codes. Nowdays, I usually see like this pattern of codes.
(Actual codes are more complex but almost same as follows)
Duplicated erase()
calls are scattered in this function.
typedef map< int, float > IF_MAP;
bool DoAndPopOld( IF_MAP& M, int K )
{
IF_MAP::iterator Itr = M.find( K );
if ( Itr == M.end() ) return false;
if ( K < 10 )
{
M.erase( Itr ); // erase call is here...
return false;
}
if ( 100 < K )
{
// Do something
M.erase( Itr ); // and here...
return true;
}
// Do something
M.erase( Itr ); // and also here!
return true;
}
So, I wanna refactoring above code like this...
class ScopedOutCaller
{
private:
boost::function< void() > F;
public:
ScopedOutCaller( boost::function< void() > _F ) : F(_F) {}
~ScopedOutCaller() { F(); } // deferred function call
};
bool DoAndPopNew( IF_MAP& M, int K )
{
IF_MAP::iterator Itr = M.find( K );
if ( Itr == M.end() ) return false;
// Make deferred call, so I do not consider calling erase function anymore.
ScopedOutCaller SOC( boost::bind( &IF_MAP::erase ), &M, Itr );
if ( K < 10 )
{
// M.erase( Itr ); <-- unnecessary
return false;
}
if ( 100 < K )
{
// Do something
// M.erase( Itr ); <-- unnecessary
return true;
}
// Do something
// M.erase( Itr ); <-- unnecessary
return true;
}
But, as I asked... compile errors are occurred. The long and the short of what I want to do is how to defer function call. Please tell me the way to make deferred call. Thanks.