gpt4 book ai didi

c++ - C++函数指针的困惑

转载 作者:太空宇宙 更新时间:2023-11-03 10:25:04 25 4
gpt4 key购买 nike

我的源文件名以 cpp 结尾。

下面是2个声明:

void (*f1)(void);

void *f2(void);

我认为:

  • f1 是一个有效的函数指针。
  • f2 是一个返回 void * 指针的函数。

那我还有一个功能:

void f3(void *p_func);

我认为:

  • 尽管有 p_func 参数名称,f3 只是一个接受 void * 指针的函数,即一个 32 位无符号整数一台 32 位机器。

以及以下 2 个陈述:

f3(&f1); //<----------It works
f3(&f2); //<----------Compiler error

编译错误是:

no known conversion from 'void *(*)()' to 'void *' for 2nd argument;

我的问题:

  • &f1应该是一个函数指针的地址,为什么它可以作为void *传递给f3
  • 为什么 &f2void *(*)()
  • void *(*)() 到底是什么意思?

最佳答案

你说得对,第一个是指向函数的指针,第二个是函数声明。

这意味着编译器对警告的判断也是正确的。

第一个调用 ( f3(&f1) ) 传递函数指针的地址,它可以转换为 void * (与我之前的评论相反)。所以,不需要错误。 (它是一个指向函数指针的指针,因此是一个数据对象,而不是函数指针。省略 & 并且您会得到与第二次调用相同的错误。)

第二次调用(f3(&f2))传递一个指向函数的指针,函数指针和void指针不可相互转换。 &在函数名前面的是多余的——而且在上下文中会产生轻微的误导。您可以添加任意数量的 * , 或单个 & (或将它们全部省略)来自函数名称并且它被视为相同 - 标准 C 的奇怪方面之一。(另请参见 Why do function pointer definitions work with any number of ampersands '&' or asterisks '*'? )

我注意到我必须使用 -pedantic让 GCC 完全提示它。这是标准在附件 J 中记录通用扩展的结果:

J.5.7 Function pointer casts

¶1 A pointer to an object or to void may be cast to a pointer to a function, allowing data to be invoked as a function (6.5.4).

¶2 A pointer to a function may be cast to a pointer to an object or to void, allowing a function to be inspected or modified (for example, by a debugger) (6.5.4).

你问什么void *(*)()方法。它是指向函数的指针的“转换”形式,该函数采用不确定的参数列表(但不是末尾带有省略号 ... 的可变参数列表)并返回指向函数的指针。


haccks asked :

Could you please add a reference from C standard about function pointers and void pointers are not inter-convertible?

是的——这很简单:

6.2.5 Types

¶1 … Types are partitioned into object types (types that describe objects) and function types (types that describe functions).

6.3 Conversions

6.3.2.3 Pointers

¶1 A pointer to void may be converted to or from a pointer to any object type. A pointer to any object type may be converted to a pointer to void and back again; the result shall compare equal to the original pointer.

¶7 A pointer to an object type may be converted to a pointer to a different object type. If the resulting pointer is not correctly aligned68) for the referenced type, the behavior is undefined. Otherwise, when converted back again, the result shall compare equal to the original pointer. When a pointer to an object is converted to a pointer to a character type, the result points to the lowest addressed byte of the object. Successive increments of the result, up to the size of the object, yield pointers to the remaining bytes of the object.

¶8 A pointer to a function of one type may be converted to a pointer to a function of another type and back again; the result shall compare equal to the original pointer. If a converted pointer is used to call a function whose type is not compatible with the referenced type, the behavior is undefined.

那些是指针类型之间唯一定义的转换。在指向对象的指针和指向函数的指针之间没有任何转换,所以它是不允许的(但不一定需要诊断;它不在标准的“约束”部分)。

测试代码

变体 1(pf19.c):

void (*f1)(void);
void *f2(void);

void f3(void *p_func);

int main(void)
{
f3(&f1);
f3(&f2);
}

编译警告(由于 -Werror-pedantic 引起的错误):

$ gcc -O3 -g -std=c11 -Wall -Wextra -Werror -Wmissing-prototypes -Wstrict-prototypes  -Wold-style-definition -pedantic -c pf19.c
pf19.c: In function ‘main’:
pf19.c:9:12: error: ISO C forbids passing argument 1 of ‘f3’ between function pointer and ‘void *’ [-Werror=pedantic]
f3(&f2); //<----------Compiler error
^
pf19.c:4:6: note: expected ‘void *’ but argument is of type ‘void * (*)(void)’
void f3(void *p_func);
^~
cc1: all warnings being treated as errors

变体 2(也是 pf19.c):

void (*f1)(void);
void *f2(void);

void f3(void *p_func);

int main(void)
{
f3(f1);
f3(&f2);
}

编译信息:

$ gcc -O3   -g      -std=c11   -Wall -Wextra -Werror -Wmissing-prototypes -Wstrict-prototypes  -Wold-style-definition      -pedantic -c pf19.c
pf19.c: In function ‘main’:
pf19.c:8:8: error: ISO C forbids passing argument 1 of ‘f3’ between function pointer and ‘void *’ [-Werror=pedantic]
f3(f1);
^~
pf19.c:4:6: note: expected ‘void *’ but argument is of type ‘void (*)(void)’
void f3(void *p_func);
^~
pf19.c:9:8: error: ISO C forbids passing argument 1 of ‘f3’ between function pointer and ‘void *’ [-Werror=pedantic]
f3(&f2);
^
pf19.c:4:6: note: expected ‘void *’ but argument is of type ‘void * (*)(void)’
void f3(void *p_func);
^~
cc1: all warnings being treated as errors
$

C 编译器与 C++ 编译器相比,消息的措辞不同,但意图是相同的( pf17.ccpf19.c 的简单拷贝):

$ g++ -O3 -g -I./inc -std=c++11 -Wall -Wextra -Werror    -c pf17.cc
pf17.cc: In function ‘int main()’:
pf17.cc:8:10: error: invalid conversion from ‘void (*)()’ to ‘void*’ [-fpermissive]
f3(f1);
^
pf17.cc:4:6: note: initializing argument 1 of ‘void f3(void*)’
void f3(void *p_func);
^~
pf17.cc:9:8: error: invalid conversion from ‘void* (*)()’ to ‘void*’ [-fpermissive]
f3(&f2);
^~~
pf17.cc:4:6: note: initializing argument 1 of ‘void f3(void*)’
void f3(void *p_func);
^~
$

测试:Mac OS X 10.11.6 El Capitan 上的 GCC 6.2.0。


感谢Dmitri对于 noting §6.3.2.3 ¶1 是相关的,附件 J.5.7。

关于c++ - C++函数指针的困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39137335/

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