折叠表达式

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

【C++ Template | 04】折叠表达式

 

 

 1 #include <iostream>
 2 #include <string>
 3 
 4 template <typename ... Ts>
 5 auto sum(Ts ... ts) {
 6     return (... + ts);
 7 }
 8 
 9 int main() {
10     std::cout << sum(1, 2, 3, 4, 5) << std::endl;; //值为 5
11 
12     std::string a {"Hello "};
13     std::string b {"World"};
14     std::string c {"!!!"};
15     std::cout << sum(a, b, c) << std::endl; //值为 "Hello World!!!"
16 }

 

相关文章:

  • 2022-12-23
  • 2022-02-05
  • 2022-01-08
  • 2022-12-23
  • 2021-09-02
  • 2021-09-06
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-01-01
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-05
  • 2021-12-18
  • 2021-10-20
相关资源
相似解决方案