2

I'm testing a C++ class with a number of functions that all have basically the same form:

ClassUnderTest t;

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

Since there's a lot of these, I thought I'd write a function to do most of the heavy lifting:

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

But, the compiler doesn't seem to agree with me that this is a good 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

I'm using Visual Studio 2008 SP1. If anybody can point out what I'm doing wrong, I would appreciate it.

Thanks, PaulH

brickner
  • 6,595
  • 3
  • 41
  • 54
PaulH
  • 7,759
  • 8
  • 66
  • 143

1 Answers1

7

The error is in your code, not in bind. You pass a functor that does not expect any arguments. Instead of your call, do

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

If you omit _1 then bind will create a zero-argument function object, and the member function (which expects a data pointer) will miss one argument when called by bind.

Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
  • I could ignore the warnings, but I was hoping that whatever I could do to fix the warnings would also produce code that wouldn't make the compiler crash. – PaulH Mar 04 '10 at 16:45
  • So, is there a way to make this valid C++03? – PaulH Mar 04 '10 at 16:49
  • 1
    @PaulH, i don't know enough about `bind` to say how it works internally, and not enough about MSVC++ to say why it crashes either. I'm sorry. Maybe another one can shed more light on the issue here. Maybe try passing `&t` instead of `ref(t)` or try passing `boost::mem_fn(&Class::Function)` to bind's first parameter instead of `&Class::Function` directly. – Johannes Schaub - litb Mar 04 '10 at 16:55
  • @Johannes Schaub - I changed to use boost::mem_fn() as you suggested. The compiler crash and warnings went away to be replaced by a single erorr on the line `fcn( &data )`: `Bind.hpp( 236 ) term does not evaluate to a function taking 1 argument.` Any ideas? – PaulH Mar 04 '10 at 17:08
  • 1
    @PaulH, ah of course. You have to tell `bind` that it expects one argument still. So you need to pass `_1` in addition: `bind(&ClassUnderTest::SomeFunction, &t, _1)`. If you omit `_1`, then the resulting function object cannot be called by passing arguments. – Johannes Schaub - litb Mar 04 '10 at 17:14
  • @PaulH, i removed the text about const qualified function types. While it explains the warnings, it's not really relevant i think, and just introduced noises. – Johannes Schaub - litb Mar 04 '10 at 17:22