سؤال

أنا أختبر فئة C ++ مع عدد من الوظائف التي تحتوي جميعها بشكل أساسي على نفس الشكل:

ClassUnderTest t;

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

نظرًا لوجود الكثير من هذه ، اعتقدت أنني سأكتب وظيفة للقيام بمعظم الرفع الثقيل:

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

لكن يبدو أن المترجم يتفق معي على أن هذه فكرة جيدة ...

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

أنا أستخدم Visual Studio 2008 SP1. إذا كان بإمكان أي شخص الإشارة إلى ما أفعله خطأ ، فسأقدر ذلك.

شكرا يا بول

هل كانت مفيدة؟

المحلول

الخطأ في الكود الخاص بك ، وليس في bind. أنت تمرر المتفوق الذي لا يتوقع أي حجج. بدلا من مكالمتك ، افعل

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

إذا حذفت _1 ومن بعد bind سيقوم بإنشاء كائن دالة صفر ، وستفتقد وظيفة العضو (التي تتوقع مؤشر بيانات) وسيطة واحدة عند استدعائها بواسطة bind.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top