Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/utility/variadic"

From cppreference.com
< cpp‎ | utility
(use templates)
m (fmt)
 
(23 intermediate revisions by 13 users not shown)
Line 1: Line 1:
 
{{title|Variadic functions}}
 
{{title|Variadic functions}}
{{cpp/utility/variadic/sidebar}}
+
{{cpp/utility/variadic/navbar}}
  
Variadic functions are functions (e.g. {{c|std::printf}}) which take a variable number of arguments.  
+
Variadic functions are functions (e.g. {{lc|std::printf}}) which take a [[cpp/language/variadic_arguments|variable number of arguments]].
  
===Usage===
+
To declare a variadic function, an ellipsis appears after the list of parameters, e.g. {{c|int printf(const char* format...);}}, which may be preceded by an optional comma. See [[cpp/language/variadic_arguments|Variadic arguments]] for additional detail on the syntax, automatic argument conversions and the alternatives.
  
To declare a variadic function, an ellipsis is used as the last parameter, e.g. {{c|int printf(const char *format, ...);}}.  Parameters passed to a variadic function can be accessed using the following macros and types:
+
To access the variadic arguments from the function body, the following library facilities are provided:
  
{{dcl list begin}}
+
{{dsc begin}}
{{dcl list header | cstdarg}}
+
{{dsc header|cstdarg}}
{{dcl list template | cpp/utility/variadic/dcl list va_start}}
+
{{dsc inc|cpp/utility/variadic/dsc va_start}}
{{dcl list template | cpp/utility/variadic/dcl list va_arg}}
+
{{dsc inc|cpp/utility/variadic/dsc va_arg}}
{{dcl list template | cpp/utility/variadic/dcl list va_copy}}
+
{{dsc inc|cpp/utility/variadic/dsc va_copy}}
{{dcl list template | cpp/utility/variadic/dcl list va_end}}
+
{{dsc inc|cpp/utility/variadic/dsc va_end}}
{{dcl list template | cpp/utility/variadic/dcl list va_list}}
+
{{dsc inc|cpp/utility/variadic/dsc va_list}}
{{dcl list end}}
+
{{dsc end}}
 
+
===Default conversions===
+
 
+
When a variadic function is called, after lvalue-to-rvalue, array-to-pointer, and function-to-pointer conversions, each argument that is a part of the variable argument list undergoes additional conversions known as ''default argument promotions'':
+
 
+
* {{c|std::nullptr_t}} is converted to {{c|void*}}
+
* {{c|float}} arguments are converted to {{c|double}} as in ''floating-point promotion''
+
* {{c|bool}}, {{c|char}}, {{c|short}}, and unscoped enumerations are converted to {{c|int}} or wider integer types as in ''integer promotion''
+
 
+
Only arithmetic, enumeration, pointer, pointer to member, and class type arguments are allowed.
+
 
+
===Alternatives===
+
 
+
* Variadic templates can also be used to create functions that take variable number of arguments.  They are often the better choice because they do not impose restrictions on the types of the arguments, do not perform integral and floating-point promotions, and are type safe. {{mark since c++11}}
+
* If all variable arguments share a common type, a {{c|std::initializer_list}} provides a convenient mechanism (albeit with a different syntax) for accessing variable arguments.
+
  
 
===Example===
 
===Example===
 
 
 
{{example
 
{{example
|
+
|code=
| code=
+
#include <cstdarg>
 
#include <iostream>
 
#include <iostream>
#include <cstdarg>
 
  
void simple_printf(const char *fmt, ...)
+
void simple_printf(const char* fmt...) // C-style "const char* fmt, ..." is also valid
 
{
 
{
 
     va_list args;
 
     va_list args;
 
     va_start(args, fmt);
 
     va_start(args, fmt);
  
     while (*fmt != '\0') {
+
     while (*fmt != '\0')
         if (*fmt == 'd') {
+
    {
 +
         if (*fmt == 'd')
 +
        {
 
             int i = va_arg(args, int);
 
             int i = va_arg(args, int);
 
             std::cout << i << '\n';
 
             std::cout << i << '\n';
         } else if (*fmt == 'c') {
+
         }
 +
        else if (*fmt == 'c')
 +
        {
 
             // note automatic conversion to integral type
 
             // note automatic conversion to integral type
 
             int c = va_arg(args, int);
 
             int c = va_arg(args, int);
 
             std::cout << static_cast<char>(c) << '\n';
 
             std::cout << static_cast<char>(c) << '\n';
         } else if (*fmt == 'f') {
+
         }
 +
        else if (*fmt == 'f')
 +
        {
 
             double d = va_arg(args, double);
 
             double d = va_arg(args, double);
 
             std::cout << d << '\n';
 
             std::cout << d << '\n';
Line 68: Line 56:
 
     simple_printf("dcff", 3, 'a', 1.999, 42.5);  
 
     simple_printf("dcff", 3, 'a', 1.999, 42.5);  
 
}
 
}
| output=
+
|output=
 
3
 
3
 
a
 
a
Line 74: Line 62:
 
42.5
 
42.5
 
}}
 
}}
 +
 +
===See also===
 +
{{dsc begin}}
 +
{{dsc see c|c/variadic|Variadic functions|nomono=true}}
 +
{{dsc end}}
 +
 +
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}

Latest revision as of 10:50, 3 January 2024

 
 
Utilities library
General utilities
Relational operators (deprecated in C++20)
 
Variadic functions
 

Variadic functions are functions (e.g. std::printf) which take a variable number of arguments.

To declare a variadic function, an ellipsis appears after the list of parameters, e.g. int printf(const char* format...);, which may be preceded by an optional comma. See Variadic arguments for additional detail on the syntax, automatic argument conversions and the alternatives.

To access the variadic arguments from the function body, the following library facilities are provided:

Defined in header <cstdarg>
enables access to variadic function arguments
(function macro) [edit]
accesses the next variadic function argument
(function macro) [edit]
(C++11)
makes a copy of the variadic function arguments
(function macro) [edit]
ends traversal of the variadic function arguments
(function macro) [edit]
holds the information needed by va_start, va_arg, va_end, and va_copy
(typedef) [edit]

[edit] Example

#include <cstdarg>
#include <iostream>
 
void simple_printf(const char* fmt...) // C-style "const char* fmt, ..." is also valid
{
    va_list args;
    va_start(args, fmt);
 
    while (*fmt != '\0')
    {
        if (*fmt == 'd')
        {
            int i = va_arg(args, int);
            std::cout << i << '\n';
        }
        else if (*fmt == 'c')
        {
            // note automatic conversion to integral type
            int c = va_arg(args, int);
            std::cout << static_cast<char>(c) << '\n';
        }
        else if (*fmt == 'f')
        {
            double d = va_arg(args, double);
            std::cout << d << '\n';
        }
        ++fmt;
    }
 
    va_end(args);
}
 
int main()
{
    simple_printf("dcff", 3, 'a', 1.999, 42.5); 
}

Output:

3
a
1.999
42.5

[edit] See also

C documentation for Variadic functions