【发布时间】:2020-06-27 14:19:11
【问题描述】:
我在使用 decltype 创建指向“foo”的 typedef 函数指针时遇到问题。 printf 有效,但有警告:warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘bar {aka int (*)(int)}’ [-Wformat=] 并且 cout 显示“1”。在函数指针方面,我有点像新手,所以我真的不知道发生了什么。有人可以帮我吗?
#include <iostream>
int foo(int a) {
return a;
}
int main() {
typedef decltype(&foo) bar;
printf("printf: %d\n", (*bar(70))); //work with a warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘bar {aka int (*)(int)}’ [-Wformat=]
std::cout << "cout: " << (*bar(80));//displays "cout: 1" for some reason
return 0;
}
【问题讨论】:
-
bar不是函数指针,它是一个类型,bar(70)是一个函数式类型转换。
标签: c++