Search code examples
c++templatesboostimplementationboost-signals2

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.


Solution

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