JWellbelove
JWellbelove

Reputation: 121

Is there a way to implement this using C++17 Fold Expressions?

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?

Upvotes: 2

Views: 88

Answers (1)

Jarod42
Jarod42

Reputation: 218098

Syntax whould be:

static bool call_handler(const MessageType& command)
{
    return (... || TMessageHandler::call_handler(command));
}

Upvotes: 6

Related Questions