【问题标题】:Is there a way to implement this using C++17 Fold Expressions?有没有办法使用 C++17 折叠表达式来实现这一点?
【发布时间】:2021-02-26 10:47:42
【问题描述】:

想象一下我有这个模板类。

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.
    }
};

我希望能够为每个TMessageHandler 类型调用静态call_handler 并返回返回值的OR。对于三种消息处理程序类型,代码将与此等价...

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);
    }
};

是否可以使用折叠表达式来实现?

【问题讨论】:

  • 你试过return ( TMessageHandler::call_handler(command) || ... )吗? (当然您需要将message_handler_set 更改为可变参数。)

标签: c++ c++17 expression fold


【解决方案1】:

语法应该是:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-29
    • 1970-01-01
    • 1970-01-01
    • 2020-01-25
    • 2020-03-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多