Domanda

Sto testando una classe C ++ con una serie di funzioni che hanno sostanzialmente la stessa forma:

ClassUnderTest t;

DATATYPE data = { 0 };
try
{
    t.SomeFunction( &data );
}
catch( const SomeException& e )
{
    // log known error
}
catch( ... )
{
    // log unknown error
}

Dato che ce ne sono molti, ho pensato di scrivere una funzione per fare la maggior parte del sollevamento pesante:

template< typename DATA, typename TestFunction >
int DoTest( TestFunction test_fcn )
{
    DATA data = { 0 };
    try
    {
        test_fcn( &data );
    }
    catch( const SomeException& e )
    {
        // log known error
        return FAIL;
    }
    catch( ... )
    {
        // log unknown error
        return FAIL;
    }
    return TRUE;
}

ClassUnderTest t;
DoTest< DATATYPE >( boost::bind( &ClassUnderTest::SomeFunction, boost::ref( t ) ) );

Ma il compilatore non sembra essere d'accordo con me sul fatto che questa è una buona idea ...

Warning 1   warning C4180: qualifier applied to function type has no meaning; ignored   c:\boost\boost_1_41_0\boost\bind\bind.hpp   1657
Warning 2   warning C4180: qualifier applied to function type has no meaning; ignored   c:\boost\boost_1_41_0\boost\bind\mem_fn.hpp 318
Warning 3   warning C4180: qualifier applied to function type has no meaning; ignored   c:\boost\boost_1_41_0\boost\bind\mem_fn.hpp 326
Warning 4   warning C4180: qualifier applied to function type has no meaning; ignored   c:\boost\boost_1_41_0\boost\bind\mem_fn.hpp 331
Warning 5   warning C4180: qualifier applied to function type has no meaning; ignored   c:\boost\boost_1_41_0\boost\bind\mem_fn.hpp 345
Warning 6   warning C4180: qualifier applied to function type has no meaning; ignored   c:\boost\boost_1_41_0\boost\bind\mem_fn.hpp 350
Warning 7   warning C4180: qualifier applied to function type has no meaning; ignored   c:\boost\boost_1_41_0\boost\bind\mem_fn.hpp 362
Error   8   fatal error C1001: An internal error has occurred in the compiler.  c:\boost\boost_1_41_0\boost\bind\mem_fn.hpp 328

Sto usando Visual Studio 2008 SP1. Se qualcuno può sottolineare cosa sto facendo di sbagliato, lo apprezzerei.

Grazie, Paulh

È stato utile?

Soluzione

L'errore è nel tuo codice, non in bind. Passano un funtore che non si aspetta argomenti. Invece della tua chiamata, fai

DoTest< DATATYPE >( boost::bind( &ClassUnderTest::SomeFunction, &t, _1) );

Se ometti _1 poi bind creerà un oggetto funzione di argomentazione zero e la funzione membro (che si aspetta un puntatore di dati) perderà un argomento quando viene chiamato da bind.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top