gpt4 book ai didi

c - _Generic assoc-list 中的函数指针类型是否没有按预期工作?

转载 作者:行者123 更新时间:2023-12-01 23:51:16 25 4
gpt4 key购买 nike

我正在尝试通过不限制函数指针参数接受具有特定类型参数的函数来“破解”类型系统。但是,我仍然想让它成为类型安全的,所以我想我会将这种“hack”与 _Generic 关键字的可能性结合起来。

我有以下四个功能:

#include <stdio.h>   /* printf() */
#include <stdlib.h> /* EXIT_SUCCESS */

static void
function_i(int *i)
{
printf("%d\n", *i);
}


static void
function_f(float *f)
{
printf("%.2ff\n", *f);
}


static void
caller(void(*func)(),
void *arg)
{
func(arg);
}


static void
except(void(*func)(),
void *arg)
{
printf("unsupported type\n");
}

第一个和第二个将传递给第三个,我想确定,如果传递给第三个的函数类型和参数不正确,那么将调用第四个函数。因此我创建了以下 _Generic 选择器:

#define handler(func, arg) _Generic((func), \
void(*)(int*): _Generic((arg), \
int* : caller, \
default : except), \
void(*)(float*): _Generic((arg), \
float* : caller, \
default : except), \
default: except)(func, arg)

然后我调用他们:

int main(void)
{
int i = 12;
float f = 3.14f;

void(*func_ptr_i)(int*) = function_i;
void(*func_ptr_f)(float*) = function_f;

handler(function_i, &i);
handler(function_f, &f);

handler(func_ptr_i, &i);
handler(func_ptr_f, &f);

return EXIT_SUCCESS;
}

输出很有趣:

unsupported type
unsupported type
12
3.14f

我希望这也适用于前两种情况,而无需为传递的函数创建特定的函数指针变量。问题是:这是 clang 的 _Generic 中的实现错误,还是这是预期的行为?是这样吗,我很好奇到底是为什么?以及如何在不创建额外函数指针的情况下使其工作?

提前致谢!


系统信息:

compiler: Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn)
flags: cc -std=c11 -Wall -v -g

最佳答案

您面临的问题是 _Generic 的选择表达式未被评估。如果是这样,您的函数名称将退化为函数指针,一切都会正常工作。

在您的选择表达式中添加一个 & 应该可以解决这个问题。

关于c - _Generic assoc-list 中的函数指针类型是否没有按预期工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26340574/

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