5

以下C++代码怎么理解,这模板怎么推导的?

 3 years ago
source link: https://www.zhihu.com/question/463875728/answer/1934003540
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

以下C++代码怎么理解,这模板怎么推导的?

[图片] [图片] 看了_Is_any_of_v的实现,但不理解is_same<_Ty, _Traits>...是怎么作为disjunction的模板…
20
4,536
登录后你可以
不限量看优质回答私信答主深度交流精彩内容一键收藏
C++程序猿, 公众号:高级开发者

请仔细观察如下两个的区别。第一个是参数包展开,第二个折叠表达式。

template<typename ...Ts>
void f(Ts&&... args) {
    auto v  = {g(args) ...}; // 参数包展开
    auto v_ = (g(args),...); // 折叠表达式
}

关键在于逗号的区别,多了一个逗号(op)变成了折叠表达式:最终结果为一个值。而没有逗号则是参数包展开,结果是多个值

你那个例子中的disjunction_v使用参数包展开形式。

具体展开形式可以参考:C++ Insights

v2-7acde9b38f1df7dae6421e8d6f834320_720w.jpg?source=1940ef5c


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK