5

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?

Mathias Soeken
  • 1,293
  • 2
  • 8
  • 22

1 Answers1

6

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 );
}
UncleBens
  • 40,819
  • 6
  • 57
  • 90
  • Thank your for both suggestions. I prefer the cast workaround in the Boost.Bind FAQ and used this now in my source code. – Mathias Soeken Feb 14 '10 at 12:57
  • For the record, the cast workaround is this: given you want to bind a function `int Foo::get() const`, instead of binding `&Foo::get`, bind `static_cast(&Foo::get)` – congusbongus Oct 20 '14 at 23:51