首页 > 解决方案 > 如何为旧的 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 错误:“{”

标签: cvisual-c++

解决方案


正如我在评论中所说,在旧编译器中确实没有很好的方法来做到这一点。

一种可能的方法是使用包装 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);

请注意,这一切都未经测试,也没有任何类型的错误检查。


推荐阅读