2

Imagine I have this template class.

template <typename... TMessageHandler>
class message_handler_set
{
public:

    static bool call_handler(const MessageType& command)
    {
         // Call each TMessageHandler type's 'call_handler' and return the OR of the returns.
    }
};

I would like to be able to call the static call_handler for each of the TMessageHandler types and return the OR of the return values. For three message handler types, the code would be the equivalent of this...

template <typename TMessageHandler1, typename TMessageHandler2, typename TMessageHandler3>
class message_handler_set
{
public:

    static bool call_handler(const MessageType& command)
    {
         return TMessageHandler1::call_handler(command) ||
                TMessageHandler2::call_handler(command) ||
                TMessageHandler3::call_handler(command);
    }
};

Is it possible to implement this using fold expressions?

JWellbelove
  • 121
  • 2
  • 6
  • Have you tried `return ( TMessageHandler::call_handler(command) || ... )`? ( Of course you will need to change `message_handler_set` to variadic. ) – therealcain Feb 26 '21 at 10:58

1 Answers1

6

Syntax whould be:

static bool call_handler(const MessageType& command)
{
    return (... || TMessageHandler::call_handler(command));
}
Jarod42
  • 203,559
  • 14
  • 181
  • 302