【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!!!"
}