c - 如何为旧的 Visual Studio 2005 转换 C 代码,尤其是 TinyExpr 源代码
问题描述
我想将 TinyExpr 的源代码(https://github.com/codeplea/tinyexpr)转换为 Visual Studio 2005 的 C 兼容代码(我认为它符合 C89/90)。特别是我在将这个宏转换为(存在于 tinyexpr.c 中)时遇到问题:
#define NEW_EXPR(type, ...) new_expr((type), (const te_expr*[]){__VA_ARGS__})
和:
typedef struct te_expr {
int type;
union {double value; const double *bound; const void *function;};
void *parameters[1];
} te_expr;
static te_expr *new_expr(const int type, const te_expr *parameters[]) {
...
}
static te_expr *base(state *s) {
...
}
问题出现在调用NEW_EXPR(TE_FUNCTION1 | TE_FLAG_PURE, base(s));
中:我有一个:
错误 C2059:sintax 错误:“{”
解决方案
正如我在评论中所说,在旧编译器中确实没有很好的方法来做到这一点。
一种可能的方法是使用包装 vararg 函数,它动态创建数组,调用真实new_expr
函数,然后释放临时数组。
也许像
te_expr *new_expr_va(const int type, ...) {
re_expr *result = NULL; /* The resulting expression structure to return */
size_t argument_count = 0; /* The number of te_expr arguments passed */
/*
* We do two passes over the arguments, once to get the number of them,
* and once to get the actual values.
*/
/* First get the number of arguments */
{
va_list va;
va_start(va, type);
/* Loop until we get a null pointer */
while (va_arg(va, te_expr *) != NULL)
++argument_count;
va_end(va);
}
/* Now allocate the array */
te_expr **parameters = malloc(argument_count * sizeof(te_expr *));
/* And populate the array */
{
va_list va;
size_t index = 0;
te_expr *expr;
va_start(va, type);
/* Get all arguments and add to the allocated array */
while ((expr = va_arg(va, te_expr *)) != NULL)
parameters[index++] = expr;
va_end(va);
}
/* Now we call the actual function */
result = new_expr(type, parameters);
/* Free the memory we allocated for the array */
free(parameters);
/* And return the result */
return result;
}
可以使用如
/* Argument list must be terminated by a NULL */
new_expr_va(TE_FUNCTION1 | TE_FLAG_PURE, base(s), NULL);
请注意,这一切都未经测试,也没有任何类型的错误检查。
推荐阅读
- .net - 通过 Docker 运行 .Net Core 应用程序时找不到 .XML 文件
- css - 如何使两个或更多行长表适合内联引导程序 4?
- msbuild - Cakebuild 脚本使用 vs 2017 并且构建失败
- javascript - 如何在 postgresql 中更改格式日期
- graphql - Graphql `Parameter \"obj\" to Document() 必须是一个对象,得到`
- python - 如何从带有参数的python脚本运行新终端?
- python - Python - 解析串行数据并查找特定字符串
- optaplanner - Red Hat Decision Manager 7.3 OptaCloud 求解器示例不工作
- neo4j - GraphFactory 无法实例化此 Graph 实现 [class org.apache.tinkerpop.gremlin.neo4j.structure.Neo4jGraph]]
- r - 合并源中的函数和重复的列名