Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Distinguish between const and non-const method with same name in boost::bind

When I use boost::bind with a method name which is declared both const and non-const I am getting in ambiguous error, for example

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

How can I solve this problem?

like image 330
Mathias Soeken Avatar asked Feb 14 '10 12:02

Mathias Soeken


1 Answers

The problem together with workarounds is descibed in FAQ part of Boost.Bind reference.

You could also make use of utility functions like the following:

#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 );
}
like image 192
UncleBens Avatar answered Nov 15 '22 13:11

UncleBens