![](/img/trans.png)
[英]Distinguish between function references/pointers accepting const and non-const argument with same name as function parameter
[英]Distinguish between const and non-const method with same name in boost::bind
當我將boost::bind
與同時聲明為const和非const的方法名一起使用時,我遇到了模棱兩可的錯誤,例如
boost::bind( &boost::optional<T>::get, _1 )
我怎么解決這個問題?
在Boost.Bind參考的FAQ部分中描述了該問題以及解決方法。
您還可以使用以下實用工具功能:
#include <boost/bind.hpp>
#include <boost/optional.hpp>
template <class Ret, class Obj>
Ret (Obj::* const_getter(Ret (Obj::*p) () const)) () const
{
return p;
}
template <class Ret, class Obj>
Ret (Obj::* nonconst_getter(Ret (Obj::*p)())) ()
{
return p;
}
int main()
{
boost::bind( const_getter(&boost::optional<int>::get), _1 );
boost::bind( nonconst_getter(&boost::optional<int>::get), _1 );
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.