Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
251 views
in Technique[技术] by (71.8m points)

Comma omitted in variadic function declaration in C++

I am used to declaring variadic functions like this:

int f(int n, ...);

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

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: error: expected ‘;’, ‘,’ or ‘)’ before ‘...’ token int f(int n...);

Is there any difference between writing int f(int n, ...) and int f(int n...)?

Why was this syntax added C++?

question from:https://stackoverflow.com/questions/35708014/comma-omitted-in-variadic-function-declaration-in-c

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 “...”.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...