Question

Je teste une classe C ++ avec un certain nombre de fonctions qui ont toutes la même forme:

ClassUnderTest t;

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

Comme il y en a beaucoup, j'ai pensé écrire une fonction pour faire la plupart du gros du travail:

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

Mais, le compilateur ne semble pas être d'accord avec moi que c'est une bonne idée ...

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

J'utilise Visual Studio 2008 SP1. Si quelqu'un peut souligner ce que je fais de mal, je l'apprécierais.

Merci, Paulh

Était-ce utile?

La solution

L'erreur est dans votre code, pas dans bind. Vous passez un fonctor qui ne s'attend à aucun argument. Au lieu de votre appel, faites

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

Si vous omettez _1 alors bind créera un objet de fonction argument zéro-argument, et la fonction membre (qui attend un pointeur de données) manquera un argument lorsqu'il est appelé par bind.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top