2

I'm using a generic EventEmitter instance:

EventEmitter mEventHandler;

So I've defined this bind:

function<void(int, double)> onSetMin = bind(&ILFO::SetMin, this, placeholders::_2);
mEventHandler.on(kParamID, onSetMin);

and the on such as:

mEventHandler.emit(paramID, someInt, someDouble);

which is "generic" as said, and set 2 params. But my specific function SetMin need just one param (which would be someDouble in this case):

void ILFO::SetMin(double min);

How would you just pass second paramter from bind?

markzzz
  • 47,390
  • 120
  • 299
  • 507

3 Answers3

2

Easier to use a lambda for your problem I think:

function<void(int, double)> onSetMin = [this](int dummy, double d) { SetMin(d); };
mEventHandler.on(kParamID, onSetMin);
Smeeheey
  • 9,906
  • 23
  • 39
1

According to C++ reference in the std::bind call you can use

unbound arguments replaced by the placeholders _1, _2, _3... of namespace std::placeholders

https://en.cppreference.com/w/cpp/utility/functional/bind

using namespace std::placeholders;  // for _1, _2, _3...
// demonstrates argument reordering and pass-by-reference
int n = 7;
// (_1 and _2 are from std::placeholders, and represent future
// arguments that will be passed to f1)
auto f1 = std::bind(f, _2, 42, _1)
atlex2
  • 2,548
  • 1
  • 14
  • 19
0

Use a lambda instead of std::bind:

mEventHandler.on(kParamID, [this] (int, double value) {
    SetMin(value);
});

The purpose of std::bind is the opposite of what you want to do: It helps you create a function taking N arguments from a function f taking M where M > N by fixing some arguments of f to given values (or/and change the order of the arguments).

Holt
  • 36,600
  • 7
  • 92
  • 139