views:

304

answers:

2

I would like to call a member through lambda::bind. Unfortunately I have got two members with the same name but different return types. Is there a way to help the lambda::bind to deduce the right return type for a member function call? (bind works fine with explicit return type deduction)

#include <vector>
#include <iostream>
#include <algorithm>
#include <boost/bind.hpp>
#include <boost/lambda/lambda.hpp>
#include <boost/lambda/bind.hpp>

using namespace std;
using namespace boost;

struct A
{
  A (const string & name) : m_name(name) {}

  string &        name ()         { return m_name; }
  const string &  name () const   { return m_name; }

  string m_name;
};

vector<A> av;

int main () 
{
  av.push_back (A ("some name"));

  // compiles fine
  find_if(av.begin(), av.end(), bind<const string &>(&A::name, _1) == "some name");

  // error: call of overloaded 'bind(<unresolved overloaded function type>, const boost::lambda::lambda_functor<boost::lambda::placeholder<1> >&)' is ambiguous
  find_if(av.begin(), av.end(), lambda::bind(&A::name, lambda::_1) == "some name");

  return 0;
}
A: 

For the documentation

"The return type of the lambda functor created by the bind expression can be given as an explicitly specified template parameter, as in the following example:

bind(target-function, bind-argument-list)"

So just do the same thing you did with boost:bind.

  find_if(av.begin(), av.end(), lambda::bind<const string &>(&A::name, lambda::_1) == "some name");

P.S. not tested

Vicente Botet Escriba
You have just pointed out the real problem :)There is a way to override the return type in lambda::boost but it only works for the lambda expression not for the bind. see: lambda::ret<T>(e) in the reference. Unfortunately in the current situation it is not applicable...
psaghelyi
A: 

The different return types is a red herring. The issue is with const overloading of method (i.e. you would have the same problem no matter what the relative return types are). This is problem is documented here and here, and using the return type-specified form is not the recommended solution (will work most of the time, except some version of MSVC).

The problem is that taking the address of an overloaded member function (either const overloaded or parameter overloaded) is ambiguous, so some extra information is required.

The solution is to cast the function pointer, which lets the compiler know exactly which of the overloaded functions you want, The cleanest way to do this I've found is to typedef the function pointer types, since otherwise the lines get a bit nasty. Here's an example with your code (compiles clean gcc 4.3.4):

#include <vector>
#include <iostream>
#include <algorithm>
#include <boost/bind.hpp>
#include <boost/lambda/lambda.hpp>
#include <boost/lambda/bind.hpp>

using namespace std;
using namespace boost;

struct A
{
  A (const string & name) : m_name(name) {}

  string &        name ()         { return m_name; }
  const string &  name () const   { return m_name; }

  string m_name;
};

vector<A> av;

//function pointer for non-const version
typedef string& (A::*NameFuncType)(void);

//function pointer for const version
typedef const string& (A::*NameConstFuncType)(void) const;

int main () 
{
  av.push_back (A ("some name"));

  //'correct' way to call const version w/ boost::bind
  find_if(av.begin(), av.end(), 
    bind(static_cast<NameConstFuncType>(&A::name), _1) == "some name"
  );

  //call for non-const version w/ boost::lambda::bind
  find_if(av.begin(), av.end(), 
     lambda::bind(static_cast<NameFuncType>(&A::name), lambda::_1) == "some name"
  );

  return 0;
}
academicRobot