c++ - 内部编译器错误和 boost::bind

标签 c++ boost bind c1001

我正在测试一个 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。 如果有人能指出我做错了什么,我将不胜感激。

谢谢, 保罗H

最佳答案

错误在您的代码中,而不是在bind 中。您传递了一个不期望任何参数的仿函数。而不是你的电话,做

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

如果省略 _1,则 bind 将创建一个零参数函数对象,并且成员函数(需要数据指针)在调用时将丢失一个参数绑定(bind)

关于c++ - 内部编译器错误和 boost::bind,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2380839/

相关文章:

c++ - C++ 头文件如何包含实现?

c++ - 如何在你的 C++ 项目中包含 boost::thread?

jquery - 使用 jQuery 将播放/暂停/结束功能绑定(bind)到 HTML5 视频

c++ - 使用 boost::bind 传递成员函数

c++ - 自定义 STL 迭代器类

c++ - 标准 vector 性能/替代

c++ - bgl 中的 edge_descriptor 映射

c++ - 如何将 boost::object_pool<>::construct 与非 const 引用一起用作 ctor 参数?

c++ - 如何将 std::function 的目标与成员函数的地址进行比较?

c++ - 初始化一个抽象类?