相关疑难解决方法(0)

使用fold表达式将参数包扩展为lambda - gcc vs clang

考虑以下代码段:

template <typename TF>
void post(TF){ }

template <typename... TFs>
struct funcs : TFs...
{
    funcs(TFs... fs) : TFs{fs}... { }

    void call() 
    { 
        (post([&]{ static_cast<TFs&>(*this)(); }), ...); 
    }
};
Run Code Online (Sandbox Code Playgroud)

clang ++ 3.8+成功编译代码.

g ++ 7.0无法编译,出现以下错误:

prog.cc: In lambda function:
prog.cc:10:43: error: parameter packs not expanded with '...':
        (post([&]{ static_cast<TFs&>(*this)(); }), ...);
                   ~~~~~~~~~~~~~~~~~~~~~~~~^~
prog.cc:10:43: note:         'TFs'
prog.cc: In member function 'void funcs<TFs>::call()':
prog.cc:10:13: error: operand of fold expression has no unexpanded parameter packs
        (post([&]{ static_cast<TFs&>(*this)(); }), …
Run Code Online (Sandbox Code Playgroud)

c++ lambda language-lawyer fold-expression c++17

6
推荐指数
1
解决办法
846
查看次数

标签 统计

c++ ×1

c++17 ×1

fold-expression ×1

lambda ×1

language-lawyer ×1