- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在使用第三方库时,我经常发现自己需要编写粘合“代码”来处理跨版本更改的函数原型(prototype)。
以 Linux 内核为例:这是一个常见的场景 - 假设我们有函数 int my_function(int param)
并且在某些时候我们需要添加一个可选的 void *data
。将以这种方式添加新参数,而不是破坏 API:
int __my_function(int param, void *data);
static inline my_function(int param) {
return __my_function(param, NULL);
}
这很好:它向不需要的人隐藏了新参数和 API 损坏。现有代码可以继续将 my_function
与旧原型(prototype)一起使用。
然而,情况并非总是如此(无论是在 Linux 中还是在其他库中),我最终得到了这样的片段,来处理我遇到的所有可能的版本:
#if LIBRARY_VERSION > 5
my_function(1, 2, 3);
#elif LIBRARY_VERSION > 4
my_function(1, 2);
#else
my_function(1);
#endif
所以我在想,对于简单的原型(prototype)更改(参数的重新排序、添加/删除“默认”参数等),让编译器自动完成会很好。
我希望自动完成此操作(无需指定确切的版本),因为有时要确定引入更改的确切库/内核版本需要大量工作。因此,如果我不关心新参数并且我可以只使用 0
,我希望编译器在需要时使用 0
。
我最远的距离是这样的:
#include <stdio.h>
#if 1
int f(int a, int b) {
return a + b;
}
#else
int f(int a) {
return a + 5;
}
#endif
int main(void) {
int ret = 0;
int (*p)() = (int(*)())f;
if (__builtin_types_compatible_p(typeof(f), int(int, int)))
ret = p(3, 4);
else if (__builtin_types_compatible_p(typeof(f), int(int)))
ret = p(1);
else
printf("no matching call\n");
printf("ret: %d\n", ret);
return 0;
}
这可行 - GCC 在编译类型中选择适当的调用,但它有两个问题:
p("a", "b")
是合法的,但给出的结果是垃圾。(这里可能还有其他问题,但这些是我认为最关键的点)
我相信它可以通过一些宏魔法来实现:如果调用参数是分开的,宏可以生成找到适当原型(prototype)的代码,然后一一测试所有给定参数的类型兼容性。但我认为它使用起来会复杂得多,所以我正在寻找一个更简单的解决方案。
有什么想法如何通过适当的类型检查+促销来实现这一点吗?我认为如果不使用编译器扩展就无法完成此任务,因此我的问题集中在针对 Linux 的现代 GCC。
编辑:在对其进行宏观化+添加Acorn的 Actor 想法后,我留下了这个:
#define START_COMPAT_CALL(f, ret_type, params, args, ret_value) if (__builtin_types_compatible_p(typeof(f), ret_type params)) (ret_value) = ( ( ret_type(*) params ) (f) ) args
#define ELSE_COMPAT_CALL(f, ret_type, params, args, ret_value) else START_COMPAT_CALL(f, ret_type, params, args, ret_value)
#define END_COMPAT_CALL() else printf("no matching call!\n")
int main(void) {
int ret = 0;
START_COMPAT_CALL(f, int, (int, int), (999, 9999), ret);
ELSE_COMPAT_CALL(f, int, (int), (1), ret);
ELSE_COMPAT_CALL(f, int, (char, char), (5, 9999), ret);
ELSE_COMPAT_CALL(f, int, (float, int), (3, 5), ret);
ELSE_COMPAT_CALL(f, int, (float, char), (3, 5), ret);
END_COMPAT_CALL();
printf("ret: %d\n", ret);
return 0;
}
这对于我注意到的两点是有效的 - 它给出类型检查警告并正确执行升级。 但是它也会对所有“未选择”的调用站点发出警告:警告:通过不兼容类型调用的函数
。我尝试用 __builtin_choose_expr
包装调用,但我没有运气:/
最佳答案
您可以使用 _Generic
在标准 C 中执行此操作:
// Define sample functions.
int foo(int a, int b) { return a+b; }
int bar(int a, int b, int c) { return a+b+c; }
// Define names for the old and new types.
typedef int (*Type0)(int, int);
typedef int (*Type1)(int, int, int );
/* Given an identifier (f) for a function and the arguments (a and b) we
want to pass to it, the MyFunctio macro below uses _Generic to test which
type the function is and call it with appropriate arguments.
However, the unchosen items in the _Generic would contain function calls
with mismatched arguments. Even though these are never evaluated, they
violate translation-time contraints for function calls. One solution would
be to cast the function (automatically converted to a function pointer) to
the type being used in the call expression. However, GCC sees through this
and complains the function is called through a non-compatible type, even
though it never actually is. To solve this, the Sanitize macro is used.
The Sanitize macro is given a function type and a function. It uses a
_Generic to select either the function (when it matches the type) or a null
pointer of the type (when the function does not match the type). (And GCC,
although unhappy with a call to a function with wrong parameters, is happy
with a call to a null pointer.)
*/
#define Sanitize(Type, f) \
_Generic((f), Type: (f), default: (Type) 0)
#define MyFunction(f, a, b) \
_Generic(f, \
Type0: Sanitize(Type0, (f)) ((a), (b)), \
Type1: Sanitize(Type1, (f)) ((a), (b), 0) \
)
#include <stdio.h>
int main(void)
{
printf("%d\n", MyFunction(foo, 3, 4));
printf("%d\n", MyFunction(bar, 3, 4));
}
上面使用参数化函数名称 (f
) 来让我们通过定义的两个函数(foo
和 bar
)来演示这一点。对于问题中的情况,只有一个函数定义,只有一个名称,因此我们可以简化宏。我们还可以使用函数名称作为宏名称。 (在预处理过程中不会递归地替换,因为 C 不会递归地替换宏,而且名称不会出现在替换文本中,其后带有左括号。)这看起来像:
#define Sanitize(Type, f) \
_Generic((f), Type: (f), default: (Type) 0)
#define foo(a, b) \
_Generic(foo, \
Type0: Sanitize(Type0, foo) ((a), (b)), \
Type1: Sanitize(Type1, foo) ((a), (b), 0) \
)
…
printf("%d\n", foo(3, 4));
(此处使用的 Sanitize
定义由 Artyer 在 this answer 中提供。)
关于根据函数原型(prototype)编译时决定调用参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63403418/
是否有任何库或框架旨在促进从另一种成熟的编程语言中构建项目? 在 C++、java 等编程语言中指定逻辑、集合和复杂规则非常容易,但在 Makefile 中完成这些事情似乎是一场艰苦的战斗。我还没有深
我有这段代码可以用 clang 编译得很好(即使使用 -Weverything),但是 gcc 会发出错误。 #include #include #include using namespace
我有以下 block 头文件 BKE_mesh.h: /* Connectivity data */ typedef struct IndexNode { struct IndexNode *
我在我的一个项目中遇到了一个奇怪的问题。我的代码库依赖于一个外部库,其中包含一个名为 Dataset 的类. Dataset类私有(private)继承自 std::vector (其中 Sample
当使用 gcc、g++ 或 make 在终端中编译一个小型 C 或 C++ 项目时,我收到以下错误: /tmp/ccG1caGi.o: In function `main': main.c:(.tex
我正在尝试从 CVS 为 Windows 上的 Emacs 23.1.50 编译 CEDET,但在“第 6 步:打开 EDE...”时出现错误:“defvar:作为变量的符号值是无效的:cedet-m
我正在(重新)学习编程,我从 C 开始。我的 IDE(如果我可以这么说)是 Windows7 上的 cygwin(32 位)和 Visual-Studio 2010。我总是编译我用 gcc (cygw
我喜欢在模板类中使用本地类来执行类似“static if”的构造。但是我遇到了 gcc 4.8 不想编译我的代码的问题。但是 4.7 可以。 这个例子: #include #include #in
我有一个项目,必须仅使用 java 1.4 进行编译。但我计划使用mockito 编写一些单元测试。我想要一种在 pom 中指定的方法,以便 src/main/java 使用 jdk 1.4 编译,但
我想了解 PHP 编译过程是如何工作的。 假设我有一个名为funcs.php 的文件并且这个文件有三个函数,如果我include 或require 它,所有的在文件加载期间编译三个函数?或者源代码会被
编译工具链 我们写程序的时候用的都是集成开发环境 (IDE: Integrated Development Environment),集成开发环境可以极大地方便我们程序员编写程序,但是配置起来
当我编写一些 Scala 代码时,在尝试编译代码时收到一条奇怪的错误消息。我将代码分解为一个更简单的代码(从语义的角度来看这完全没有意义,但仍然显示了错误)。 scala> :paste // Ent
我正在编译一个 SCSS 文件,它似乎删除了我的评论。我可以使用什么命令来保留所有评论? >SASS input.scss output.css 我在 SCSS 中看到两种类型的注释。 // Comm
这是我的代码: #include typedef struct { const char *description; float value; int age; } swag
当您编译 grails war 时,我知道 .groovy 代码被编译为字节码类文件,但我不明白容器(例如 tomcat)如何在请求 GSP 时知道如何编译它们。容器了解 GSP 吗?安装在服务器上的
我正在努力将多个文件编译成一个通用程序。我收到一个错误: undefined reference to 'pi' 这是我的代码和 Makefile 的框架。我做错了什么?谢谢! 文件:calcPi.c
我尝试使用 LD_PRELOAD 来 Hook sprintf function ,所以我将打印到缓冲区的结果: #define _GNU_SOURCE #include #include int
我正在寻找最简单的方法来自动将 CoffeeScript 重新编译为 JS。 阅读documentation但仍然很难得到我想要的东西。 我需要它来监视文件夹 src/ 中的任何 *.coffee 文
我想使用定制waveformjs 。我发现this on SO但是,我不知道如何编译/安装波形来开始。我从 GitHub 克隆它并进行了更改,但是我不知道如何将其转换为 .js 文件。 最佳答案 为了
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
我是一名优秀的程序员,十分优秀!