Tuesday, November 22, 2011


A colleague recently asked for suggestions on how to pass a pointer to a callback function without having the caller know whether the function is a free function or member function.  I suggested std::function (previously know as std::tr1::function or boost::function) but later realized I'd never actually used it.  Seems like now might be a good time for an experiment.

First, let's define a class that others might want to observe:
struct Caller
  typedef std::function< void() > Callback;
  void RegisterCallback( Callback const & callback )
    callbacks.push_back( callback );
  void Notify() const
    std::for_each( callbacks.begin(), callbacks.end(), []( Callback const & callback )
  std::vector< Callback > callbacks;
Next, a free function we want to be called:
void GlobalFunction()
  std::cout << "GlobalFunction" << std::endl;
As well as a functor:
struct Functor
  void operator()()
    std::cout << "Functor" << std::endl;
And finally, a member function on an observing class:
struct Observer
  void Update()
    std::cout << "Observer::Update" << std::endl;
Hooking everything up:
  Caller caller;
  Observer observer;
  caller.RegisterCallback( &GlobalFunction );
  caller.RegisterCallback( Functor() );
  caller.RegisterCallback( std::bind( &Observer::Update, observer ) );
Gives the output we'd expect:
But how could we implement an UnregisterCallback method on the Caller class? std::function is neither LessThanComparable nor EqualityComparable so storing them in a set (ordered or otherwise) won't work. Seems that Boost.Signals might be a better fit for this task.


tadghsadeghi said...

Caesars Palace Resort Casino Scheduled To Open On - JeTHub
Caesars Palace Resort Casino in Las Vegas is 강원도 출장샵 scheduled to open on 춘천 출장안마 March 26, 경주 출장안마 2020. Caesars Entertainment 세종특별자치 출장샵 announced on Wednesday 순천 출장안마 that

Unknown said...

1xbet | 1xbet | Bet with a Bonus - RMC | Riders Casino
1XBet allows you https://deccasino.com/review/merit-casino/ to 바카라 bet on any favourite 1xbet 먹튀 horse races or any other https://jancasino.com/review/merit-casino/ sporting event. ✓ Get up to £300 + 200 Free Spins ventureberg.com/ No Deposit

Unknown said...

Casinos in Malta - Filmfile Europe
Find the best Casinos communitykhabar in Malta including poormansguidetocasinogambling.com bonuses, games, games and wooricasinos.info the history of games. We filmfileeurope.com cover all the main reasons to visit Casinos in

lr9aqv4xy6 said...

A knowledge normal used by most machine monitoring options which has gained floor within the USA identified as} MTConnect. Today many new CNC machine tools come geared up to supply knowledge in this format. Older machines can still provide useful info with adapters. Machine monitoring for CNC machines has become mainstreamed just inside the last few|the earlier few|the earlier couple of} years, and new software program options are at all times in improvement. Computer Numerical Control machining is a subtractive manufacturing course of that allows best teapots with infuser producers to accurately and cost-effectively create high-precision components. CNC machining removes extra raw materials from workpieces with slicing tools to create a precise completed product from materials like plastic, steel, and composites.

u73rgo9s5p said...

The finest on-line casinos do not always have the most important libraries around, however they do tend to to|are inclined to} have a diverse mix of video games that may attraction to a wide base of players. That 메리트카지노 variety is often delivered method of|by means of|by the use of} game style, nevertheless it additionally be|may additionally be|can be} demonstrated through the use of a wholesome mixture of various software builders. Taking our primary spot is Ignition, boasting a incredible mobile poker expertise along with many different casino video games. There are different casino apps we expect you need to} a glance at|check out} too, all on this article.

Post a Comment