- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试通过不限制函数指针参数接受具有特定类型参数的函数来“破解”类型系统。但是,我仍然想让它成为类型安全的,所以我想我会将这种“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/
给定以下源代码: #include #include #include static inline void closedirp(DIR **p) { if (*p) c
在 C11 中,我可以创建一个原型(prototype)如下所示的函数: void myVaFunc(const char* const conv, ...); 我可以这样运行它: myVaFunc(
我正在阅读这个 C11 的 _Generic维基百科上的例子: #define cbrt(X) _Generic((X), long double: cbrtl, \
我需要在 C 中使用 _Generic,以便能够根据变量类型返回专门的值。 const char *encode_char_ptr(char *x) { return (x ? x : "NU
所以我正在尝试实现“通用打印宏”: #include #include #define CHECK(x) printf(#x " =" \ _Generic((x), float:
我试图学习如何使用“新的”C11 泛型表达式,但我遇到了困难。 考虑以下代码: #include #include #define test(X, Y, c) \ _Generic((X)
如何向 c11 _Generic Functions 添加额外类型? 您是否必须#undef/re-#define 它?(如果是这样,下面的工作是否可行)还是有更好的方法? #define to_st
我对 C11 的 _Generic 机制感到高兴 - 类型切换是我在 C++ 中怀念的东西。然而,事实证明它很难编写。 例如,给定函数: bool write_int(int); bool write
有没有办法在同一表达式中多次使用 _Generic 关键字来创建单个字符串文字? 我正在寻找的是一种生成单一格式字符串以传递给 printf 的方法,所有转换说明符都适合适当的类型。 写this时答案
我有一个这样的模板: template.h ---------- // Declare a function "func_type()" void JOIN(func_, T)(T t) { retu
我正在使用 C 的在线编译器和调试器。此网页使用 GCC 5.4.1 C99。当我测试这段代码时, https://www.onlinegdb.com/online_c_compiler #inclu
在下面的代码中: struct Person { char* name; int age; }; struct Book { char* title; char* au
我正在设置一些大宽度整数类型,因此我大量使用宏来使这些类型尽可能像基本整数类型一样可用。我一直遇到的一个问题是,如果我在宏扩展中大量使用 _Generic 表达式,而不是最小化 _Generic 使用
我想在C中组合两种类型来调用函数(例如具有不同列和行的乘法 vector 和矩阵): #define CC_FIRST(a) _Generic((a), int: int8d) #def
我有兴趣编写一个类型验证宏,它只在类型不是 int/short/long 或指针时发出警告。 我遇到的问题是指针可以是任何类型的指针。 #define INT_OR_POINTER_AS_UINTPT
我似乎无法将参数传递给需要不同参数的函数(或传递给实现第一个类型的子集的其他 _Generic 宏)。 #define DEBUG_PRINT(x,...) _Generic((x),
可能我在这里做错了一些非常基本的事情,但我无法弄清楚那可能是什么。我可以解释我的代码片段,但我认为我在这里尝试做的事情非常明显:为我的 DynamicArray 的 dynarr_printf 创建一
我正在尝试在 C11 中使用 _Generic 宏生成重载函数,并且我已经停止了对零参数函数的支持,例如: #define msg(_1) _Generic((_1), char*: msg_stri
使用 gcc.exe(Rev3,由 MSYS2 项目构建)8.2.0。 我试图构建一个宏来自动在两种类型之间进行类型转换,其中两个参数永远不应该是同一类型。我的问题是如果我不包含相同类型的情况,编译器
我想使用 C11 _Generic 关键字根据静态类型填充 union ,如: typedef union { double d; long l; const char*s;
我是一名优秀的程序员,十分优秀!