{"id":1311,"date":"2015-04-15T22:54:08","date_gmt":"2015-04-15T21:54:08","guid":{"rendered":"http:\/\/netrix.org.pl\/?p=1311"},"modified":"2015-04-15T22:54:08","modified_gmt":"2015-04-15T21:54:08","slug":"variadic-arguments","status":"publish","type":"post","link":"https:\/\/netrix.org.pl\/index.php\/2015\/04\/15\/variadic-arguments\/","title":{"rendered":"Variadic arguments"},"content":{"rendered":"<p>Old C++ and C for a long time allow creating functions with unknown number of arguments. Most common example of such function is <em>printf<\/em> which prints text with formatted variables. It gets format as first argument and values to be printed in that format in other arguments. Following code shows its declaration:<\/p>\n<pre lang=\"cpp\">int printf(const char * restrict format, ... );\r\n<\/pre>\n<p>Processing such argument list is done in runtime and it utilizes special macros or types from following list<a href=\"#footnote_0_1311\" id=\"identifier_0_1311\" class=\"footnote-link footnote-identifier-link\" title=\"http:\/\/en.cppreference.com\/w\/cpp\/language\/variadic_arguments\">1<\/a>:<\/p>\n<ul>\n<li><em>va_start<\/em> &#8211; enable access to variadic function arguments,<\/li>\n<li><em>va_arg<\/em> &#8211; accesses the next variadic function argument,<\/li>\n<li><em>va_copy<\/em> &#8211; makes a copy of the variadic function arguments,<\/li>\n<li><em>va_end<\/em> &#8211; ends traversal of the variadic function arguments,<\/li>\n<li><em>va_list<\/em> &#8211; holds the information needed by va_start, va_arg, va_end, and va_copy.<\/li>\n<\/ul>\n<p>Following example presents simple usage of such function:<\/p>\n<pre lang=\"cpp\" line=\"1\">\r\n#include <iostream>\r\n#include <cstdarg>\r\n \r\ndouble sum(int count, ...) \r\n{\r\n    double sum = 0.0;\r\n    va_list args;\r\n    va_start(args, count);\r\n    for (int i = 0; i < count; ++i) \r\n    {\r\n        sum += va_arg(args, double);\r\n    }\r\n    va_end(args);\r\n    return sum;\r\n}\r\n \r\nint main() \r\n{\r\n    std::cout << sum(4, 25.0, 27.3, 26.9, 25.7);\r\n}\r\n<\/pre>\n<p>Firstly <em>va_start<\/em> binds arguments passed to the function by elipsis (...) to variable <em>args<\/em> of type <em>va_list<\/em>. It takes instance of such type and number of arguments it want to access. Then all arguments are accessed subsequently by macro <em>va_arg<\/em> which takes list as an argument and returned type. At the end of processing arguments list should be unbound with macro <em>va_end<\/em>. To process same list of arguments more than once it can be copied to another list right after processing is started with macro <em>va_copy<\/em> (see<a href=\"#footnote_1_1311\" id=\"identifier_1_1311\" class=\"footnote-link footnote-identifier-link\" title=\"http:\/\/en.cppreference.com\/w\/cpp\/language\/variadic_arguments\">2<\/a> for more).<\/p>\n<p>Of course presented code is just an example. Real implementation of such function should utilize <em>std::accumulate<\/em>. Also this code shows some pitfalls of variadic arguments - number of arguments must be passed as one of firsts arguments and may not match the exact number of arguments passed to the function, also types can be different than expected in method.<\/p>\n<ol class=\"footnotes\"><li id=\"footnote_0_1311\" class=\"footnote\"><a href=\"http:\/\/en.cppreference.com\/w\/cpp\/language\/variadic_arguments\" title=\"Variadic arguments\" target=\"_blank\">http:\/\/en.cppreference.com\/w\/cpp\/language\/variadic_arguments<\/a> [<a href=\"#identifier_0_1311\" class=\"footnote-link footnote-back-link\">&#8617;<\/a>]<\/li><li id=\"footnote_1_1311\" class=\"footnote\"><a href=\"http:\/\/en.cppreference.com\/w\/cpp\/language\/variadic_arguments\" title=\"Variadic arguments\" target=\"_blank\">http:\/\/en.cppreference.com\/w\/cpp\/language\/variadic_arguments<\/a> [<a href=\"#identifier_1_1311\" class=\"footnote-link footnote-back-link\">&#8617;<\/a>]<\/li><\/ol>","protected":false},"excerpt":{"rendered":"<p>Old C++ and C for a long time allow creating functions with unknown number of arguments. Most common example of such function is printf which prints text with formatted variables. It gets format as first argument and values to be printed in that format in other arguments. Following code shows its declaration: int printf(const char [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[176,177,175],"_links":{"self":[{"href":"https:\/\/netrix.org.pl\/index.php\/wp-json\/wp\/v2\/posts\/1311"}],"collection":[{"href":"https:\/\/netrix.org.pl\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/netrix.org.pl\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/netrix.org.pl\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/netrix.org.pl\/index.php\/wp-json\/wp\/v2\/comments?post=1311"}],"version-history":[{"count":21,"href":"https:\/\/netrix.org.pl\/index.php\/wp-json\/wp\/v2\/posts\/1311\/revisions"}],"predecessor-version":[{"id":1332,"href":"https:\/\/netrix.org.pl\/index.php\/wp-json\/wp\/v2\/posts\/1311\/revisions\/1332"}],"wp:attachment":[{"href":"https:\/\/netrix.org.pl\/index.php\/wp-json\/wp\/v2\/media?parent=1311"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/netrix.org.pl\/index.php\/wp-json\/wp\/v2\/categories?post=1311"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/netrix.org.pl\/index.php\/wp-json\/wp\/v2\/tags?post=1311"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}