【问题标题】:Fold over a function with || operator用 || 折叠函数操作员
【发布时间】:2018-08-31 14:24:59
【问题描述】:

我试图将一个函数应用于参数包中的每种类型,然后折叠结果。

示例: https://godbolt.org/z/0YNon_

#include <cstdint>
#include <string>

template<typename T>
bool CheckCondition(std::uint64_t i){
    return i>sizeof(T);
}

template <typename... ToCheck>
bool AnyCheckCondition(std::uint64_t i){
    return CheckCondition<ToCheck>(i)||...;
}

int main() 
{
    return AnyCheckCondition<std::string,std::uint64_t, std::int64_t>(10);
}

很遗憾,折叠表达式无法编译,我不确定为什么或如何修复它。

我收到以下错误

<source>: In function 'bool AnyCheckCondition(uint64_t)':

<source>:11:35: error: parameter packs not expanded with '...':

     return CheckCondition<ToCheck>(i) || ...;

            ~~~~~~~~~~~~~~~~~~~~~~~^~~

<source>:11:35: note:         'ToCheck'

<source>:11:38: error: expected ';' before '||' token

     return CheckCondition<ToCheck>(i) || ...;

                                      ^~~

                                      ;

<source>:11:39: error: expected primary-expression before '||' token

     return CheckCondition<ToCheck>(i) || ...;

                                       ^~

Compiler returned: 1

【问题讨论】:

  • 请详细说明isn't particularly appreciated
  • 不编译
  • return (CheckCondition&lt;ToCheck&gt;(i) || ...); - 你忘了括号。

标签: c++ templates gcc visual-c++ c++17


【解决方案1】:

折叠表达式需要括号。

return (CheckCondition<ToCheck>(i) || ...);

【讨论】:

  • VTC 简单错字
【解决方案2】:

折叠表达式具有以下形式

( pack op ... )
( ... op pack )
( pack op ... op init )
( init op ... op pack ) 

您缺少那些外括号。将代码更改为

return (CheckCondition<ToCheck>(i) || ...);

会编译它。

【讨论】:

  • VTC 简单错字
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-03
  • 1970-01-01
  • 2011-12-02
  • 2021-12-30
  • 1970-01-01
相关资源
最近更新 更多