mercredi 25 janvier 2017

How to boost::bind a template member function which takes a universal reference as a parameter

I've been trying to use boost::bind to post a call to a member function onto an io_strand but have been getting errors. I have manged to create a simplistic equivalent example of what I am trying to do and have seen the same error in the following context:

I have the following class containing the doThings() member function I want to call:

class base
{
public:
  int x = 1;

  template<typename B>
  void doThings(B&& b)
  {}
};

There is then a subclass of this (to accurately represent the scenario I am encountering my error - I don't think it makes a difference)

class child : public base
{
  int y = 2;
};

I have the following code trying to make the boost::bind call:

template<typename A>
void getInt(child c, A&& a)
{
  boost::bind((void(child::*)(A)) &child::doThings, &c, std::forward<A>(a))();
}

And then this is being called as follows:

int main()
{
  child c = child();
  getInt(c, 7);
}

When I compile the above code I get the following error:

error: no matches converting function ‘doThings’ to type ‘void (class child::*)(int)’


If I change the function signature of doThings() to take a regular B type rather than a universal reference i.e. B rather than B&& then it compiles a runs with no issues.
I suspect my issue is something to with the cast I'm doing in getInt():

(void(child::*)(A))

But I don't know what I would need to change it to. A&& wouldn't work in this context as I believe it would represent an r-value reference in that context. The compilation error I get when I try it seems to confirm this:

error: cannot bind ‘int’ lvalue to ‘int&&’

For completeness: if I don't attempt to perform a cast then I get the following compilation error:

error: no matching function for call to ‘bind(unresolved overloaded function type, child*, int)’

Could someone please enlighten me on what I would need to do in order to make my boost::bind call valid in this scenario please?

I am using C++11

Aucun commentaire:

Enregistrer un commentaire