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

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

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

are being processed? I can see from the implementation details that the signature becomes a template parameter, which is exactly named Signature . However, I do not understand the syntax. Is this syntax permitted by the C ++ standard? How does a signal β€œstore” a function signature when it is provided in this form?

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

+6
source share
2 answers

Yes, this syntax is allowed; the type that it denotes is a reference to a function that takes bool and double and returns void . If we were a typedef , this would be inconvenient, because the name would be in the middle:

 typedef void Signature(bool, double); 

With the new syntax, the alias becomes somewhat more readable:

 using Signature = void(bool, double); 

A parallel with pointers to functions will be:

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

Subsequently, there are template tricks to separate the different elements (extract the return type and the type of each argument).

+2
source
 // 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; 

Additional information on how typedef can be used with function pointers: Typedef function pointer?

0
source

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


All Articles