【C++编程】fold expression(折叠表达式)

折叠表达式

表格4.1列出所有可能的表达式:

 

#include <iostream>
#include <string>

template <typename ... Ts>
auto sum(Ts ... ts) {
    return (... + ts);
}

int main() {
    std::cout << sum(1, 2, 3, 4, 5) << std::endl;; //值为 5

    std::string a {"Hello "};
    std::string b {"World"};
    std::string c {"!!!"};
    std::cout << sum(a, b, c) << std::endl; //值为 "Hello World!!!"
}

 

posted @ 2021-09-12 21:13  苏格拉底的落泪  阅读(87)  评论(0编辑  收藏  举报