gpt4 book ai didi

c++ - 没有命名参数的可变参数函数

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:34:51 25 4
gpt4 key购买 nike

我注意到,GCC 和 MSVC 都对以下代码感到满意:

#include <iostream>
void foo(...);

int main()
{
foo();
}

void foo(...)
{
std::cout << "foo\n";
}

更具体地说,代码在 GCC 6.2.0 和 Visual Studio 2015 下运行。

我知道 C 至少需要一个 named 参数在省略号之前,这允许使用专门的 va_start 处理任意数量的参数, va_args , 和 va_end来自 <stdarg.h> 的宏(此处为 <cstdarg>)标题。否则,它甚至无法编译。

C++ 是否对“纯省略号”形式有一些特殊处理,或者它是否不适合获取参数,即它是允许的,但完全不切实际?

最佳答案

C++ Variadiac arguments are explained here . C++ 支持此语法,但参数不可访问:

In the C programming language, at least one named parameter must appear before the ellipsis parameter, so printz(...); is not valid.

In C++, this form is allowed even though the arguments passed to such function are not accessible, and is commonly used as the fallback overload in SFINAE, exploiting the lowest priority of the ellipsis conversion in overload resolution. This syntax for variadic arguments was introduced in 1987 C++ without the comma before the ellipsis. When C89 adopted function prototypes from C++, it replaced the syntax with one requiring the comma. For compatibility, C++98 accepts both C++-style f(int n...) and C-style f(int n, ...)

关于c++ - 没有命名参数的可变参数函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39664453/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com