5

例如,当我使用boost::bind声明为 const 和 non-const 的方法名称时,我遇到了模棱两可的错误

boost::bind( &boost::optional<T>::get, _1 )

我怎么解决这个问题?

4

1 回答 1

6

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 );
}
于 2010-02-14T12:49:02.313 回答