gpt4 book ai didi

c++ - 一劳永逸地理解 C 和 C++ 中 f() 和 f(void) 之间的区别

转载 作者:行者123 更新时间:2023-11-30 16:12:44 27 4
gpt4 key购买 nike

好吧,我听到了关于这个问题的不同意见,只是想确保我理解正确。

对于 C++

声明 void f();void f(void); 的含义完全相同,函数 f 不接受任何参数。定义同上。

对于 C

声明void f(void);表示f不带任何参数。

声明 void f(); 意味着函数 f 可能有参数,也可能没有,如果有,我们不知道这些参数是什么类型,或者有多少个。请注意,它与省略号不同,我们不能使用 va_list

现在事情变得有趣了。

案例1

声明:

void f();

定义:

void f(int a, int b, float c)
{
//...
}

案例2

声明:

void f();

定义:

void f()
{
//...
}

问题:

当我们使用正确的参数、错误的参数和根本没有参数调用 f 时,在情况 1 和 2 中,编译时会发生什么?运行时会发生什么?

附加问题:

如果我用参数声明 f ,但在没有参数的情况下定义它,会有什么不同吗?我应该能够处理函数体中的参数吗?

最佳答案

更多术语(C,而不是 C++):函数的原型(prototype)声明其参数的类型。否则该函数没有原型(prototype)。

void f();                      // Declaration, but not a prototype
void f(void); // Declaration and prototype
void f(int a, int b, float c); // Declaration and prototype

不是原型(prototype)的声明是 ANSI C 之前、K&R C 时代的遗留物。使用旧式声明的唯一原因是保持与旧代码的二进制兼容性。例如,在GTK中2 有一个没有原型(prototype)的函数声明——它是偶然出现的,但是在不破坏二进制文件的情况下无法将其删除。 C99标准注释:

6.11.6 Function declarators

The use of function declarators with empty parentheses (not prototype-format parametertype declarators) is an obsolescent feature.

建议:我建议使用 -Wstrict-prototypes 编译 GCC/Clang 中的所有 C 代码和-Wmissing-prototypes ,除了通常的-Wall -Wextra .

发生了什么

void f(); // declaration
void f(int a, int b, float c) { } // ERROR

声明与函数体不一致!这实际上是一个编译时错误,这是因为你不能有 float没有原型(prototype)的函数中的参数。您无法使用 float 的原因在非原型(prototype)函数中是因为当您调用这样的函数时,所有参数都会使用某些默认提升来提升。这是一个固定的示例:

void f();

void g()
{
char a;
int b;
float c;
f(a, b, c);
}

在此程序中,a晋升为int 1c晋升为double 。所以 f() 的定义必须是:

void f(int a, int b, double c)
{
...
}

参见 C99 6.7.6 第 15 段,

If one type has a parameter type list and the other type is specified by afunction declarator that is not part of a function definition and that contains an emptyidentifier list, the parameter list shall not have an ellipsis terminator and the type of eachparameter shall be compatible with the type that results from the application of thedefault argument promotions.

答案 1

What happens at compile time in cases 1 and 2 when we call f with the correct arguments, wrong arguments and no arguments at all? What happens at run time?

当您调用f()时,使用默认促销来提升参数。如果提升的类型与 f() 的实际参数类型匹配,那么一切都很好。如果它们不匹配,它可能会编译,但你肯定会得到未定义的行为。

“未定义的行为”是“我们不保证会发生什么”的规范。也许你的程序会崩溃,也许它会正常工作,也许它会邀请你的姻亲来吃晚饭。

有两种方法可以在编译时获取诊断信息。如果您有一个具有跨模块静态分析功能的复杂编译器,那么您可能会收到一条错误消息。您还可以使用 GCC 获取非原型(prototype)函数声明的消息,使用 -Wstrict-prototypes -- 我建议在您的所有项目中打开它(使用 GTK 2 的文件除外)。

答案 2

If I declare f with arguments, but define it without them, will it make a difference? Should I be able to address the arguments from the function body?

它不应该编译。

异常

实际上有两种情况允许函数参数与函数定义不一致。

  1. 可以通过char *到一个需要 void * 的函数,反之亦然。

  2. 可以将有符号整数类型传递给需要该类型的无符号版本的函数,反之亦然,只要该值可以用两种类型表示(即,它不是负数,并且不超出有符号类型的范围)。

脚注

1:可能 char晋升为unsigned int ,但这非常罕见。

关于c++ - 一劳永逸地理解 C 和 C++ 中 f() 和 f(void) 之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58263856/

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