2010-04-12 80 views
2

我有一個Visual Studio 2008的C++應用程序,確實是這樣的:的boost ::綁定,這已經是必然

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>  ] 

感謝, PaulH

+1

也許編譯器不能找出'void main()'的東西。 – avakar 2010-04-12 20:47:45

回答

5

你需要指定模板參數。

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

編輯:此外,人們必須使用boost ::保護,以防止從FCN得到評估。請參閱Paul對正確用法的評論。

+0

謝謝!只是一個錯誤去。不能將參數1從'int'轉換爲'boost :: _ bi :: bind_t PaulH 2010-04-12 21:06:44

+2

你非常接近。我以前從來沒有聽說過boost :: protect,但是我把它搞砸了。我需要的是'Foo(boost :: protect(boost :: bind(:: GetSystemPowerStatusEx,&status,true)));' – PaulH 2010-04-12 21:47:03

+1

是的,它沒有很好的記錄。起初,我嘗試使用boost :: bind(條形碼,boost :: ref(fcn))();來解決評估問題。我很快搜索了boost :: bind中是否有解決方案,並在郵件列表中找到了一篇文章(http://lists.boost.org/boost-users/2004/11/8412.php)。但是,正如你已經知道的那樣,這也不是正確的答案。 – kloffy 2010-04-12 22:19:14

3

問題是Bar不是一個函數。然而,Bar<Fcn>是。嘗試

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

嘗試 的boost ::綁定(酒吧< FCN>,FCN)();

相關問題