What does this variadic template code do?

前端 未结 2 1752
北荒
北荒 2020-11-28 09:24
template  
void for_each_argument(F f, Args&&... args) { 
    [](...){}((f(std::forward(args)), 0)...); 
}
<         


        
2条回答
  •  悲&欢浪女
    2020-11-28 10:11

    Actually it calls function f for each argument in args in unspecified order.

    [](...){}
    

    create lambda function, that does nothing and receives arbitrary number of arguments (va args).

    ((f(std::forward(args)), 0)...)
    

    argument of lambda.

    (f(std::forward(args)), 0)
    

    call f with forwarded argument, send 0 to lambda.

    If you want specified order you can use following thing:

    using swallow = int[];
    (void)swallow{0, (f(std::forward(args)), 0)...};
    

提交回复
热议问题