C ++中可变参数函数声明中省略了逗号 [英] Comma omitted in variadic function declaration in C++
问题描述
我习惯于这样声明变量函数:
I am used to declaring variadic functions like this:
int f(int n, ...);
在阅读 C ++编程语言时,我发现书中的声明省略逗号:
When reading The C++ Programming Language I found that the declarations in the book omit the comma:
int f(int n...); // the comma has been omitted
似乎这种语法是C ++特有的,当出现以下错误时我尝试使用C编译器进行编译:
It seems like this syntax is C++ specific as I get this error when I try to compile it using a C compiler:
test.c:1:12:错误:预期为' ;',','或')'在'...'令牌之前
int f(int n ...);
写 int f(int n,...)
和 int f( int n ...
)?
为什么要在C ++中添加此语法?
Why was this syntax added C++?
推荐答案
根据§ C ++标准的8.3.5.4(当前草案):
According to § 8.3.5.4 of the C++ standard (current draft):
在语法上正确的地方,其中 ...不属于
的抽象声明符,,...
Where syntactically correct and where "..." is not part of an abstract-declarator, ", ..." is synonymous with "...".
简而言之,用C ++ ...
(省略号)本身就是一个运算符,因此可以不使用逗号而使用,但是保留了逗号的使用以实现向后兼容。
In short, in C++ ...
(ellipsis) is an operator in its own right and so can be used without the comma, but use of the comma is retained for backwards compatibility.
这篇关于C ++中可变参数函数声明中省略了逗号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!