More natural boost :: bind alternative?

Don't get me wrong: Boost is bind()great.

But I really hate writing and reading code with it, and I gave up the hope that my colleagues will someday want / use it.

I get code like this:

btn.clicked.connect(bind(&BetBar::placeBet, this, bet_id));
animator.eachFrame.connect(bind(&Widget::move, buttons[bet_id]));

Which, although logical, is very far from what I would call good code.

To demonstrate ... in C ++ 1x, we get the following:

btn.clicked.connect([&](int bet_id){ placeBet(bet_id); })
animator.eachFrame.connect([&](Point newPos) { buttons[bet_id].move(newPos) })

And a good DSL might look something like this:

on(btn.clicked) placeBet(bet_id);
on(animator.eachFrame) buttons[bet_id].move(eachFrame::newPos);

How do you deal with binding in C ++? Do you just live with what is pushing you?

+3
source share
3 answers

It seems you want the following:

  • Implicit binding to this
  • , , .
  • -.

++ , this , , , . , . , .

:

button.clicked.handler = bind(BetBar::placeBet, this, bet_id);

handler.operator=(boost::function<void(*)()> const&)

, . . boost _1 , , . _1 . , boost:: arg < 1 > . , animator.eachFrame.newPos :

animator.eachFrame.handler = bind(&Widget::move, buttons[bet_id], _1)
animator.eachFrame.handler = bind(&Widget::move, buttons[bet_id], animator.eachFrame.newPos)
+3

, pre-0x ++. Boost.Lambda Phoenix , , .

, DSL ++, boost:: proto ( ?), , - , .

: ++ (: ), , . , , , , - .

, Lua ( , ), - . , .

+2

, DSL :

btn.clicked { |bet_id| placeBet bet_id }
animator.eachFrame { |newPos| buttons[bet_id].move newPos }

: , bind .

0

Source: https://habr.com/ru/post/1702548/


All Articles