带有省略号的函数定义

Function definition with an ellipsis

我对参数个数可变的函数的语义有误解。 6.5.2.2(p6):

If the function is defined with a type that includes a prototype, and either the prototype ends with an ellipsis (, ...) or the types of the arguments after promotion are not compatible with the types of the parameters, the behavior is undefined.

这个不是很清楚。原型下面的函数定义中是否以省略号结尾?

void foo(int i, ...){
    //...
}

一开始以为是函数参数表只包含省略号的问题,比如

void foo(...){
    //...
}

但是这种情况是由6.9.1(p8)描述的:

If a function that accepts a variable number of arguments is defined without a parameter type list that ends with the ellipsis notation, the behavior is undefined

所以我不太明白他们说的

是什么意思

either the prototype ends with an ellipsis (, ...)

6.5.2.2(p6)

本段讨论了函数调用表达式提到一个没有原型的函数的情况。在这种情况下,编译器必须根据调用的参数类型猜测其参数类型。

如果该函数被定义(可能在不同的翻译单元中)为可变参数,或者具有与编译器猜测不匹配的参数类型,则行为未定义。