- 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/
以下代码,我使用 chrome 浏览器控制台进行了检查: function A(){ this.a='a' } 这是一个构造函数。我已经将一个属性 b 赋给了 A 的原型(prototype)。
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
关闭。这个问题是opinion-based .它目前不接受答案。 想改进这个问题?更新问题,以便 editing this post 提供事实和引用来回答它. 5年前关闭。 Improve this
关闭。这个问题是opinion-based .它目前不接受答案。 想改进这个问题?更新问题,以便 editing this post 提供事实和引用来回答它. 3年前关闭。 Improve this
我已经开始阅读 The Pragmatic Programmer,我很喜欢并学习堆形式,但我很难理解示踪子弹和原型(prototype)之间的区别。跟踪项目符号是否像拥有应用程序的所有 View 但尚
尽管阅读了 StackOverflow 上的大多数文章,但我现在实际上对原型(prototype)非常困惑。 function Foo() { } Foo.prototype.speak = func
我正在阅读以下代码,并开始想知道 Rectangle.prototype = Object.create(Shape.prototype) 和 Rectangle.prototype = Shape.
我想知道它们之间的区别: childObj.prototype = Object.create(parentObj.prototype) 和 childObj.prototype = parentOb
这个问题在这里已经有了答案: Why wouldn't I use Child.prototype = Parent.Prototype rather than Child.prototype =
在 node.js 中导出原型(prototype)的首选方法是什么?您可以采用两种方法: 导出原型(prototype)本身 function A () { } module.exports = A
我正在学习 JavaScript,发现了两种分配原型(prototype)的方法。 第一个是A.prototype = B.prototype,第二个是A.prototype = new B() 例如
在一些构造函数的定义之后,例如 child ,我见过以下两种形式: Child.prototype = Parent.prototype; 或 Child.prototype = new Parent
我正在阅读一本关于 OOP javascript 的书,但被其中一个示例卡住了。 在示例代码的第一个版本中,Shape 的一个新实例构造函数被创建并且 toString方法被调用。 toString方
这个问题在这里已经有了答案: What should I connect to the child prototype property in JavaScript (2 个答案) 关闭 8 年前。
在进行原型(prototype)设计时,您在多大程度上放弃了最佳实践来支持代码和修复黑客攻击?当然,代码并不打算在完整的生产环境中保留。 补充:我正在研究一个用 Python 制作的相当大的半工作原型
我正在尝试使用 Prototype 更新隐藏表单字段的值。表单域: 我正在尝试使用原型(prototype)更新值: var additionalVal = ',2'; var itemId = $
我正在阅读How to Make a Javascript Library我发现了作者所说的一个观点: function _() { //Some obects and var
我想用一个新函数扩展“Number”类型,因此我必须定义一个原型(prototype)。当我想到这一点时,我得到了一堆问题: Number 是否既继承了 Object.prototype 又继承了 F
这里好像有区别... 假设我们有 function MyConstructor() {} MyConstructor 的[[Prototype]] 是Function.prototype,不是 MyC
有人建议 Derived.prototype = Object.create(Base.prototype); 优于 Derived.prototype = new Base(); (如 this S
我是一名优秀的程序员,十分优秀!