我有以下无法编译的代码,尤其是在通过 std::forward 转发之后
struct TestParent
{
template< typename Fn >
bool test( Fn&& fn )
{
//.. do something
//.. check some condition
bool someCondition = true;
if ( someCondition )
{
//this call works!
return fn();
}
return testAtNextLevel( std::forward< Fn >( fn ) );
}
template < typename Fn >
bool testAtNextLevel( Fn&& fn )
{
if ( (this->*fn() )
{
return true;
}
//... test some more
return true;
}
}
struct TestChild: public TestParent
{
bool thisTestOk();
bool testAll();
}
bool TestChild::thisTestOk()
{
return true;
}
bool testAll()
{
auto myFunc = std::bind( &TestChild::thisTestOk, this );
return test( myFunc );
}
编译时我收到此错误消息:
error: no match for 'operator->*' (operand types are 'TestParent*' and 'std::_Bind<std::_Mem_fn<bool (TestChild::*)()>(TestChild*)>')
if ( (this->*fn)() )
任何人都知道为什么在通过 std::forward 后,该函数无法被调用?在基类中,在调用'testAtNextLevel'之前,如果满足某些条件,我们可以调用传入的函数,而不是在它被转发到另一个模板函数之后?