Cannot convert parameter 1 from "overloaded function" to "..."

Now I'm trying to use boost bind and mem_fn. But there is a problem linking an overloaded function. How to resolve the compilation error of the following 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 goal is to compile the following 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 );

Compilation error messages are as follows:

error C2665: 'boost :: mem_fn': neither of the two overloads can convert all types of arguments can be 'boost :: _ mfi :: mf1 boost :: mem_fn :: iterator> (R (__thiscall std :: map <_Kty , _Ty> :: *) (A1)) 'or' boost :: _ mfi :: cmf1 boost :: mem_fn :: iterator> (R (__thiscall std :: map <_Kty, _Ty> :: *) (A1) const) '

PS As you know U, std :: map has 3 overloaded member functions

  • void erase(iterator _Where)
  • size_type erase(const key_type& _Keyval)
  • void erase(iterator _First, iterator _Last) 2- , .


:

, . , , .

. . ( , , ) erase() .

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;
}

, ...

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;
}

, , . , , - . , , . .

+5
2

std::map - erase() , - . Boost.Bind FAQ.

. size_type erase(const key_type&):

typedef IF_MAP::size_type (IF_MAP::*EraseType2)(const IF_MAP::key_type&);
boost::function<void (const IF_MAP::key_type&)> bmf2;
bmf2 = boost::bind((EraseType2)&IF_MAP::erase, &M, _1);

, , , :

// 1. void erase(iterator position) :
typedef void (IF_MAP::*EraseType1)(IF_MAP::iterator);
boost::function<void (IF_MAP::iterator)> bmf1;
bmf1 = boost::bind((EraseType1)&IF_MAP::erase, &M, _1);

// 3. void erase(iterator first, iterator last) :
typedef void (IF_MAP::*EraseType3)(IF_MAP::iterator, IF_MAP::iterator);
boost::function<void (IF_MAP::iterator, IF_MAP::iterator)> bmf3;
bmf3 = boost::bind((EraseType3)&IF_MAP::erase, &M, _1, _2);

Visual Studio ++ 03 ( ...), :

typedef IF_MAP::iterator (IF_MAP::*EraseType1)(IF_MAP::const_iterator);
typedef IF_MAP::iterator (IF_MAP::*EraseType3)(IF_MAP::const_iterator,
                                               IF_MAP::const_iterator);

VC8 VC9 , _HAS_STRICT_CONFORMANCE, VC10 ++ 0x erase() , Dinkumware (. N3092, 23.4.1).
, ; , , VC, , .

, - Boosts shared_ptr . . VC:

typedef IF_MAP::iterator (IF_MAP::*EraseType)(IF_MAP::const_iterator);
boost::shared_ptr<void> guard(static_cast<void*>(0),
                              boost::bind((EraseType)&IF_MAP::erase, &M, Itr));
+3

:

ScopedOutCaller SOC(boost::bind(static_cast< IF_MAP::iterator (IF_MAP::*)(IF_MAP::const_iterator) >(&IF_MAP::erase), &M, Itr));

Visual Studio 2005:

ScopedOutCaller SOC(boost::bind(static_cast< IF_MAP::iterator (IF_MAP::*)(IF_MAP::iterator) >(&IF_MAP::erase), &M, Itr));
0

All Articles