可变函数声明中是可选的

可变函数声明中是可选的

本文介绍了为什么逗号在C ++可变函数声明中是可选的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这两个声明有区别吗?

int foo( int a, ... );

int foo( int a ... );

如果没有区别,使第二个语法有效的意义是什么?

If there is no difference, what was the point of making the second syntactically valid?

推荐答案

这是猜测,但在C ++中可以有一个没有其他参数的函数,例如 void f(...)而在C中这样的函数没有使用(我知道)所以 ...

This is speculation, but in C++ in can make sense to have a function with no other parameters, e.g. void f(...) whereas in C a function like this has no use (that I know of) so ... must follow some other parameter and hence, a comma.

从语法的角度来看,简单地允许 void f int a ...),并赋予它明显的意义,而不是不允许它,它不会导致编译器编写器的任何负担或任何混乱的程序员。

From a grammar point of view, it's simpler to simply allow void f( int a ... ) and give it the obvious meaning than it is to disallow it and it's not going to cause much of a burden on compiler writers or any confusion for programmers.

(我原来认为这可能与使参数包的语法更规则,但我发现它是明确允许在任何情况下C ++ 03)。

(I originally thought it might be something to do with making the grammar for parameter packs more regular but I discovered that it was explicitly allowed in C++03 in any case.)

这篇关于为什么逗号在C ++可变函数声明中是可选的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 08:36