Variadic functions

来自cppreference.com

 
 
實用工具庫
類型的支持 (basic types, RTTI, type traits)
動態內存管理
錯誤處理
程序實用工具
可變參數函數
日期和時間
函數對象
initializer_list(C++11)
bitset
hash(C++11)
關係運算符
Original:
Relational operators
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
rel_ops::operator!=
rel_ops::operator>
rel_ops::operator<=
rel_ops::operator>=
雙和元組
Original:
Pairs and tuples
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
pair
tuple(C++11)
piecewise_construct_t(C++11)
piecewise_construct(C++11)
掉期,遠期和移動
Original:
Swap, forward and move
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
swap
forward(C++11)
move(C++11)
move_if_noexcept(C++11)
declval(C++11)
 
 
可變參數函數是一個可變數目的參數(例如std::printf).
Original:
Variadic functions are functions (e.g. std::printf) which take a variable number of arguments.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

目錄

[编辑] 用法

要聲明一個可變參數函數,省略號作為最後一個參數,例如: int printf(const char *format, ...);。通過一個可變參數的函數的參數,可以使用下面的宏和類型的訪問
Original:
To declare a variadic function, an ellipsis is used as the last parameter, e.g. int printf(const char *format, ...);. Parameters passed to a variadic function can be accessed using the following macros and types:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Defined in header <cstdarg>
帶可變參數函數的參數可以訪問
Original:
enables access to variadic function arguments
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(函數宏) [edit]
訪問下一個可變參數函數的參數
Original:
accesses the next variadic function argument
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(函數宏) [edit]
(C++11)
makes a copy of the variadic function arguments
(函數宏) [edit]
結束遍歷可變參數函數的參數
Original:
ends traversal of the variadic function arguments
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(函數宏) [edit]
持有所需的信息,由va_start中,va_arg時,va_end,並va_copy
Original:
holds the information needed by va_start, va_arg, va_end, and va_copy
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(類) [edit]

[编辑] 默認的轉換

當一個可變參數函數被調用後,左值,右值,數組,指針,函數指針轉換,每個參數是可變參數列表的一部分進行額外的轉換被稱為「默認參數促銷」 ',
Original:
When a variadic function is called, after lvalue-to-rvalue, array-to-pointer, and function-to-pointer 轉換, each argument that is a part of the variable argument list undergoes additional conversions known as default argument promotions:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
  • std::nullptr_t轉換void*
    Original:
    std::nullptr_t is converted to void*
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • float參數被轉換為double浮點推廣
    Original:
    float arguments are converted to double as in 浮點推廣
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • boolcharshortint,無作用域的枚舉轉換為整型提升或更廣泛的整數類型
    Original:
    bool, char, short, and unscoped enumerations are converted to int or wider integer types as in 整型提升
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
只有算術,枚舉,指針,指向成員和允許類的類型參數.
Original:
Only arithmetic, enumeration, pointer, pointer to member, and class type arguments are allowed.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[编辑] 替代品

  • 可變參數模板,也可用於創建功能,採取可變數目的參數。他們往往是更好的選擇,因為他們不限制類型的參數,不執行整數和浮點的促銷活動,並且是類型安全的。 (C++11 起)
    Original:
    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. (C++11 起)
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • 如果所有可變的參數都有一個共同的類型,std::initializer_list訪問變數參數提供了一個方便的機制(儘管用不同的語法).
    Original:
    If all variable arguments share a common type, a std::initializer_list provides a convenient mechanism (albeit with a different syntax) for accessing variable arguments.
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.

[编辑] 為例

#include <iostream>
#include <cstdarg>
 
void simple_printf(const char *fmt, ...)
{
    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