c++ - How do I identify the last variable argument? -


this question has answer here:

so in function want take variable number of char pointers.

void myfunction(char *_name, ...); 

only, when iterate through arguments code breaks because i'm not sure how stop on last argument pass , not keep going.

void myclass::myfunction(char *_name, ...) {         char *a;     std::vector<char*> names;     va_list variablepathlist;     va_start (variablepathlist, _name);      = _name;      bool exitloop = false;     while(!exitloop)     {         names.emplace_back(a);         = va_arg(variablepathlist, char *);          //what must set "exitloop = true" @ right time?     }      va_end (variablepathlist); } 

say pass 2 char pointers myfunction, code breaks on third loop when va_arg calling on unreadable memory.

without telling myfunction how many agruments have given how can find out number set exitloop before code breaks?

i can not seem find straight forward information on how printf() , this, there lie solution i'm after.

really, thank in advance.

printf parsing format string. other functions accepting explicit count argument before varargs. way go depend on specific needs.

really, though, it's 2015 should using variadic templates rather archaic c customs. not advise storing char* in containers.

here all need:

#include <vector> #include <string>  template <typename... t> void foo(t... args) {         std::vector<std::string> names{args...};     // use `names` } 

(live demo)


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -