Gnosophilon
Gnosophilon

Reputation: 1369

How are signal signatures in `boost::signals2` implemented?

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.

Upvotes: 7

Views: 548

Answers (2)

Matthieu M.
Matthieu M.

Reputation: 299910

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).

Upvotes: 2

Arun
Arun

Reputation: 2092

// 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?

Upvotes: 0

Related Questions