I just can't find out the way to call methods of a class without using std::bind, because i need to call this method with parameter inside tryfoo.
//simple function
uint16_t getreg(const uint8_t& num)
{
return 0;
}
Lets suppose that in class ClassItem we have public method
uint16_t ClassItem::getregI(const uint8_t &f)
{
return 1;
}
function with callable function
void tryfoo (const uint8_t ¶m, std::function<uint16_t (const uint8_t&)> f)
{
// let's suppose that param= got some other value here
uint16_t result = f(param);
}
void basefunction(ClassItem &A)
{
tryfoo (0, getreg); // Why it's OK
tryfoo (0, A.getregI) // And this's NOT ?
}
This is because std::function
holds space for one pointer*, and nothing else. Passing just a plain function is possible because it fits in the space of one pointer. This is not possible with a member function from an object because you'd have to store both the pointer to the function and the pointer to the object, and you just can't fit two pointers into the space of one pointer (at least, not safely).
So you have to build a separate object with std::bind
and pass the pointer to that in. You could just pass the pointer to the member function without binding to an object like so tryfoo (0, ClassItem::getregI);
, but that wouldn't have access to the A
object.
*: the details on this is fuzzy, but the overall point still stands.