【发布时间】:2010-02-14 12:28:07
【问题描述】:
当我将 boost::bind 与声明为 const 和 non-const 的方法名称一起使用时,我遇到了模棱两可的错误,例如
boost::bind( &boost::optional<T>::get, _1 )
我该如何解决这个问题?
【问题讨论】:
标签: c++ boost boost-bind
当我将 boost::bind 与声明为 const 和 non-const 的方法名称一起使用时,我遇到了模棱两可的错误,例如
boost::bind( &boost::optional<T>::get, _1 )
我该如何解决这个问题?
【问题讨论】:
标签: c++ boost boost-bind
Boost.Bind 参考的常见问题部分中描述了该问题以及解决方法。
您还可以使用以下实用功能:
#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 );
}
【讨论】:
int Foo::get() const,而不是绑定&Foo::get,绑定static_cast<int(Foo::*)() const>(&Foo::get)