c++ - C++11 accessing each variadic parameter -


this question has answer here:

is possible split variadic parameter list of items , access them? below can see example of want achieve - first piece, standard, simple example of using variadic parameters

float sum() {     return 0; }  template<typename ... types> float sum(float first, types ... rest) {     return first + sum(rest...); } 

below, can see i'd do

template<typename ... types> float func(int first, types ... rest) {     std::cout<<first<<std::endl;     for(auto item : rest)              cout<<rest<<std::endl; } 

i know can print , things recursively, there way access params w/o recursion?

#include <iostream> #include <string>  template<class...t> void printall(const t&...t) {     using expander = int[];     auto sep = "";     (void) expander { 0, ((std::cout << sep << t), sep = ", ", 0)... };     std::cout << std::endl; }  using namespace std;  auto main() -> int {     auto s = "world"s;     auto p = "hello";     printall(p, s, 5.0, 6);     return 0; } 

expected output:

hello, world, 5, 6 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -