yohhoyの日記

技術的メモをしていきたい日記

C++11と6個のドット

C++11の文法上、ドット(.)を6連続で記述できる箇所が存在する。本記事の内容は C++11's six dots by Louis Brandy に基づく。

template <class... T>
void f(T......);  // !?

これは下記コードと等価。C++可変長引数テンプレートのfunction parameter packを表すellipsis(...)と、C言語スタイルの可変引数リストを表すellipsis(, ...)を並べ、かつ後者をカンマを省略した...で記述している。

template <class... T>
void f(T... , ...);

N3337 8.3.5/p3-4より一部引用。

3 A type of either form is a function type.
parameter-declaration-clause:
 parameter-declaration-listopt ...opt
 parameter-declaration-list , ...
(snip)
4 (snip) If the parameter-declaration-clause terminates with an ellipsis or a function parameter pack (14.5.3), the number of arguments shall be equal to or greater than the number of parameters that do not have a default argument and are not function parameter packs. Where syntactically correct and where "..." is not part of an abstract-declarator, ", ..." is synonymous with "...". (snip)

おまけ:C言語との微妙な差異

プログラミング言語C++での構文定義と異なり、C言語では可変引数リスト ellipsis より前に1個以上の引数とカンマが必須となっている。(下記コードはC++98/03/11の全てでwell-formed)

void f(int, ...);
// OK: Cでもwell-formed

void g1(int...);  // カンマ省略
void g2(...);     // ellipsisのみ
// NG: いずれもCではill-formed

ISO C99 6.7.5/p1より構文定義を一部引用。

direct-declarator:
 direct-declarator ( parameter-type-list )
 (snip)
parameter-type-list:
 parameter-list
 parameter-list , ...
(snip)