内部编译器错误和 boost::bind
我正在测试一个带有许多函数的 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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
错误出现在您的代码中,而不是在
bind
中。您传递一个不需要任何参数的函子。而不是你的调用,如果你省略
_1
thenbind
将创建一个零参数函数对象,并且成员函数(需要一个数据指针)将错过一个参数当被bind
调用时。The error is in your code, not in
bind
. You pass a functor that does not expect any arguments. Instead of your call, doIf you omit
_1
thenbind
will create a zero-argument function object, and the member function (which expects a data pointer) will miss one argument when called bybind
.