Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      Variadic arguments

      From cppreference.com
      <cpp‎ |language
       
       
      C++ language
      General topics
      Flow control
      Conditional execution statements
      Iteration statements (loops)
      Jump statements
      Functions
      Function declaration
      Lambda function expression
      inline specifier
      Dynamic exception specifications(until C++17*)
      noexcept specifier(C++11)
      Exceptions
      Namespaces
      Types
      Specifiers
      constexpr(C++11)
      consteval(C++20)
      constinit(C++20)
      Storage duration specifiers
      Initialization
      Expressions
      Alternative representations
      Literals
      Boolean -Integer -Floating-point
      Character -String -nullptr(C++11)
      User-defined(C++11)
      Utilities
      Attributes(C++11)
      Types
      typedef declaration
      Type alias declaration(C++11)
      Casts
      Memory allocation
      Classes
      Class-specific function properties
      Special member functions
      Templates
      Miscellaneous
       
       

      Allows a function to accept any number of extra arguments.

      A function is a variadic if the last parameter of itsparameter list is an ellipsis (...).

      The comma preceding the ellipsis can be omitted.(deprecated in C++26)
      // the function declared as followsint printx(constchar* fmt, ...);int printx(constchar* fmt...);// same as above, but deprecated since C++26 // may be called with one or more arguments:printx("hello world");printx("a=%d b=%d", a, b); int printy(...,constchar* fmt);// error: ... can only be the last parameterint printz(...);// valid, but the arguments cannot be accessed portably

      This is different from a functionparameter pack expansion, which is indicated by an ellipsis that is a part of a parameter declarator, rather than an ellipsis being a parameter alone. Both parameter pack expansion and the “variadic” ellipsis may appear in the declaration of a function template, as in the case ofstd::is_function.

      (since C++11)

      Contents

      [edit]Default argument promotions

      When a variadic function is called, after lvalue-to-rvalue, array-to-pointer, and function-to-pointerconversions, each argument that is a part of the variable argument list undergoes additional conversions known asdefault argument promotions:

      (since C++11)

      Non-POD class types(until C++11)Scoped enumerations and class types with an eligible non-trivial copy constructor, an eligible non-trivial move constructor, or a non-trivial destructor(since C++11) are conditionally-supported in potentially-evaluated calls with implementation-defined semantics (these types are always supported inunevaluated calls).

      Because variadic parameters have the lowest rank for the purpose ofoverload resolution, they are commonly used as the catch-all fallbacks inSFINAE.

      Within the body of a function that uses variadic arguments, the values of these arguments may be accessed using the<cstdarg> library facilities:

      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 byva_start,va_arg,va_end, andva_copy
      (typedef)[edit]

      The behavior of theva_start macro is undefined if the last parameter before the ellipsis has reference type, or has type that is notcompatible with the type that results from default argument promotions.

      If the apack expansion or an entity resulting from alambda capture is used as the last parameter inva_start, the program is ill-formed, no diagnostic required.

      (since C++11)

      [edit]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.
      • If all variable arguments share a common type, astd::initializer_list provides a convenient mechanism (albeit with a different syntax) for accessing variable arguments. In this case however the arguments cannot be modified sincestd::initializer_list can only provide a const pointer to its elements.
      (since C++11)

      [edit]Notes

      In the C programming language until C23, at least one named parameter must appear before the ellipsis parameter, soR printz(...); is not valid until C23. In C++, this form is allowed even though the arguments passed to such function are not accessible, and is commonly used as the fallback overload inSFINAE, exploiting the lowest priority of the ellipsis conversion inoverload resolution.

      This syntax for variadic arguments was introduced in 1983 C++ without the comma before the ellipsis. When C89 adopted function prototypes from C++, it replaced the syntax with one requiring the comma. For compatibility, C++98 accepts both C++-stylef(int n...) and C-stylef(int n, ...). The original C++-style grammar is deprecated since C++26.

      The comma can be used in abbreviated function templates to make the ellipsis signify a variadic function instead of a variadic template:

      void f1(auto...);  // same as template<class... Ts> void f3(Ts...)
      void f2(auto, ...);// same as template<class T> void f3(T, ...)

      (since C++20)

      [edit]Defect reports

      The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

      DRApplied toBehavior as publishedCorrect behavior
      CWG 506C++98passing non-POD class arguments to an
      ellipsis resulted in undefined behavior
      passing such arguments is
      conditionally-supported with
      implementation-defined semantics
      CWG 634C++98conditionally-supported class types
      made some SFINAE idioms not work
      always supported if unevaluated
      CWG 2247C++11no restriction on passing parameter
      pack or lambda capture tova_start
      made ill-formed,
      no diagnostic required
      CWG 2347C++11it was unclear whether scoped enumerations passed to
      an ellipsis are subject to default argument promotions
      passing scoped enumerations
      is conditionally-supported with
      implementation-defined semantics

      [edit]See also

      C documentation forVariadic arguments
      C documentation forImplicit conversions
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/language/variadic_arguments&oldid=179642"

      [8]ページ先頭

      ©2009-2025 Movatter.jp