名前空間
変種
操作

Variadic functions

提供: cppreference.com
< cpp‎ | utility

 
 
ユーティリティライブラリ
支援を入力します (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、とすぐ分かることで必要な情報を保持します
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_tvoid*に変換されます
    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.
  • boolcharshort、および範囲外の列挙はintのよう整数昇格以上の幅の整数型に変換されます
    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