2

我有一个 Visual Studio 2008 C++ 应用程序,它执行以下操作:

template< typename Fcn >
inline void Bar( Fcn fcn )  // line 84
{
    fcn();
};

template< typename Fcn >
inline void Foo( Fcn fcn )
{
    // this works fine
    Bar( fcn );

    // this fails to compile
    boost::bind( Bar, fcn )();
};

int main()
{
    SYSTEM_POWER_STATUS_EX status = { 0 };
    Foo( boost::bind( ::GetSystemPowerStatusEx, &status, true ) );  // line 160
    return 0;
}

*调用 GetSystemPowerStatusEx() 仅用于演示。在那里插入你最喜欢的电话,行为是一样的。

当我去编译这个时,我得到 84 个错误。除非被问到,否则我不会全部发布它们,但它们是从这个开始的:

1>.\MyApp.cpp(99) : error C2896: 'boost::_bi::bind_t<_bi::dm_result<MT::* ,A1>::type,boost::_mfi::dm<M,T>,_bi::list_av_1<A1>::type> boost::bind(M T::* ,A1)' : cannot use function template 'void Bar(Fcn)' as a function argument
1>        .\MyApp.cpp(84) : see declaration of 'Bar'
1>        .\MyApp.cpp(160) : see reference to function template instantiation 'void Foo<boost::_bi::bind_t<R,F,L>>(Fcn)' being compiled
1>        with
1>        [
1>            R=BOOL,
1>            F=BOOL (__cdecl *)(PSYSTEM_POWER_STATUS_EX,BOOL),
1>            L=boost::_bi::list2<boost::_bi::value<_SYSTEM_POWER_STATUS_EX *>,boost::_bi::value<bool>>,
1>            Fcn=boost::_bi::bind_t<BOOL,BOOL (__cdecl *)(PSYSTEM_POWER_STATUS_EX,BOOL),boost::_bi::list2<boost::_bi::value<_SYSTEM_POWER_STATUS_EX *>,boost::_bi::value<bool>>>
1>        ]

如果有人能指出我可能做错了什么,我将不胜感激。


编辑:通过更改为:

boost::bind( Bar< Fcn >, fcn );

正如大家所建议的那样,我遇到了 1 个错误:

1>boost/bind/bind.hpp(246) : error C2664: 'void (Fcn)' : cannot convert parameter 1 from 'int' to 'boost::_bi::bind_t<R,F,L>'
1>        with
1>        [
1>            Fcn=boost::_bi::bind_t<BOOL,BOOL (__cdecl *)(PSYSTEM_POWER_STATUS_EX,BOOL),boost::_bi::list2<boost::_bi::value<_SYSTEM_POWER_STATUS_EX *>,boost::_bi::value<bool>>>
1>        ]
1>        and
1>        [
1>            R=BOOL,
1>            F=BOOL (__cdecl *)(PSYSTEM_POWER_STATUS_EX,BOOL),
1>            L=boost::_bi::list2<boost::_bi::value<_SYSTEM_POWER_STATUS_EX *>,boost::_bi::value<bool>>
1>        ]
1>        No constructor could take the source type, or constructor overload resolution was ambiguous
1>        boost/bind/bind_template.hpp(20) : see reference to function template instantiation 'void boost::_bi::list1<A1>::operator ()<void(__cdecl *)(Fcn),boost::_bi::list0>(boost::_bi::type<T>,F &,A &,int)' being compiled
1>        with
1>        [
1>            A1=boost::_bi::bind_t<BOOL,BOOL (__cdecl *)(PSYSTEM_POWER_STATUS_EX,BOOL),boost::_bi::list2<boost::_bi::value<_SYSTEM_POWER_STATUS_EX *>,boost::_bi::value<bool>>>,
1>            Fcn=boost::_bi::bind_t<BOOL,BOOL (__cdecl *)(PSYSTEM_POWER_STATUS_EX,BOOL),boost::_bi::list2<boost::_bi::value<_SYSTEM_POWER_STATUS_EX *>,boost::_bi::value<bool>>>,
1>            T=void,
1>            F=void (__cdecl *)(boost::_bi::bind_t<BOOL,BOOL (__cdecl *)(PSYSTEM_POWER_STATUS_EX,BOOL),boost::_bi::list2<boost::_bi::value<_SYSTEM_POWER_STATUS_EX *>,boost::_bi::value<bool>>>),
1>            A=boost::_bi::list0
1>        ]
1>        boost/bind/bind_template.hpp(18) : while compiling class template member function 'void boost::_bi::bind_t<R,F,L>::operator ()(void)'
1>        with
1>        [
1>            R=void,
1>            F=void (__cdecl *)(boost::_bi::bind_t<BOOL,BOOL (__cdecl *)(PSYSTEM_POWER_STATUS_EX,BOOL),boost::_bi::list2<boost::_bi::value<_SYSTEM_POWER_STATUS_EX *>,boost::_bi::value<bool>>>),
1>            L=boost::_bi::list1<boost::_bi::bind_t<BOOL,BOOL (__cdecl *)(PSYSTEM_POWER_STATUS_EX,BOOL),boost::_bi::list2<boost::_bi::value<_SYSTEM_POWER_STATUS_EX *>,boost::_bi::value<bool>>>>
1>        ]
1>        .\MyApp.cpp(99) : see reference to class template instantiation 'boost::_bi::bind_t<R,F,L>' being compiled
1>        with
1>        [
1>            R=void,
1>            F=void (__cdecl *)(boost::_bi::bind_t<BOOL,BOOL (__cdecl *)(PSYSTEM_POWER_STATUS_EX,BOOL),boost::_bi::list2<boost::_bi::value<_SYSTEM_POWER_STATUS_EX *>,boost::_bi::value<bool>>>),
1>            L=boost::_bi::list1<boost::_bi::bind_t<BOOL,BOOL (__cdecl *)(PSYSTEM_POWER_STATUS_EX,BOOL),boost::_bi::list2<boost::_bi::value<_SYSTEM_POWER_STATUS_EX *>,boost::_bi::value<bool>>>>
1>        ]
1>        .\MyApp.cpp(160) : see reference to function template instantiation 'void Foo<boost::_bi::bind_t<R,F,L>>(Fcn)' being compiled
1>        with
1>        [
1>            R=BOOL,
1>            F=BOOL (__cdecl *)(PSYSTEM_POWER_STATUS_EX,BOOL),
1>            L=boost::_bi::list2<boost::_bi::value<_SYSTEM_POWER_STATUS_EX *>,boost::_bi::value<bool>>,
1>            Fcn=boost::_bi::bind_t<BOOL,BOOL (__cdecl *)(PSYSTEM_POWER_STATUS_EX,BOOL),boost::_bi::list2<boost::_bi::value<_SYSTEM_POWER_STATUS_EX *>,boost::_bi::value<bool>>>
1>        ]

谢谢,保罗

4

3 回答 3

5

您需要指定模板参数。

boost::bind( Bar<Fcn>, fcn )();

编辑:此外,必须使用 boost::protect 来防止 fcn 被评估。有关正确用法,请参阅保罗的评论。

于 2010-04-12T20:48:37.807 回答
3

问题是这Bar不是一个函数。Bar<Fcn>然而是。尝试

boost::bind( Bar<Fcn>, fcn )();
于 2010-04-12T20:49:13.467 回答
1

尝试 boost::bind( Bar< Fcn >, fcn )();

于 2010-04-12T20:51:47.627 回答