6

I have been using boost::signals2 for some time now in my projects. To my shame, I still do not understand how they are implemented under the hood. My problems already start at the very definition of a signal. How is a definition such as

boost::signals2::signal< void (bool, double) > 

handled? I can see from the implementation details that the signature becomes a template parameter that is aptly named Signature. However, I do not understand the syntax. Is this syntax permitted by the C++ standard? How can a signal "store" the function signature when it is provided in this form?

I already tried looking at the sources, but was unable to find an explanation for this syntax. Any help would be appreciated.

Gnosophilon
  • 1,340
  • 7
  • 24
  • I'd guess `Signature` is used as function signature. google-mock uses the same thing to describe and bind function signatures of mocked interfaces, apparently it's valid syntax. – πάντα ῥεῖ May 15 '13 at 07:17

2 Answers2

2

Yes, this syntax is allowed; the type it denotes is a reference to function taking a bool and double and returning void. If we were to typedef it, it would be awkward, because the name would sit in the middle:

typedef void Signature(bool, double);

With the new alias syntax is becomes slightly more readable:

using Signature = void(bool, double);

The parallel with pointers to functions would be:

typedef void (*Pointer)(bool, double);

using Pointer = void (*)(bool, double);

Afterwards, there are template tricks to tease apart the various elements (extract the return type, and the type of each argument).

Matthieu M.
  • 287,565
  • 48
  • 449
  • 722
  • Thank you! That is what I was looking for. Maybe that's too broad a question, but how can I look up/learn this stuff myself? I tried reading the C++ standard document myself, but is there an "easier" way? – Gnosophilon May 21 '13 at 06:53
  • 1
    The C++ standard is perhaps the harder way about it. There are several "tutorial" or "advanced" books, if you look up at the description of the C++ tag you'll get a list of useful questions on this site. One, notably, is a [list of books](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – Matthieu M. May 21 '13 at 06:58
0
// In header: <boost/signals2/signal.hpp>
template<typename Signature, ... /* Omitted other args for clarity */> 
class signal : public boost::signals2::signal_base {
public:

  // In your case, typedef would be evaluated as
  //typedef void (bool, double) signature_type    
  typedef Signature  signature_type;    

More info on how typedef can be used with function pointers: Typedef function pointer?

Community
  • 1
  • 1
Arun
  • 2,087
  • 2
  • 20
  • 33