替换宏变量中的字符串?

发布于 2024-09-29 15:05:57 字数 1088 浏览 1 评论 0原文

我有一个宏,我在其中传递一个参数并使用它根据输入的名称定义一个新变量:

#define DO_X(x) char _do_x_var_ ## x; /* other things */

问题是如果我传递一个结构变量,它会中断:

DO_X(some_struct->thing)

变成:

char _do_x_var_some_struct->thing; /* other things */

编辑:我希望它评估什么是:(

char _do_x_var_some_struct__thing; /* other things */

或任何包含与输入类似的内容的有效变量名)

我真正想要的是让这些工作:

#define DO_X(x) for(char _do_x_var_ ## x; /*things*/)
DO_X(x){
    DO_X(y) {
        /*things*/
    }
}

DO_X(object->x){
    DO_X(object->y) {
        /*things*/
    }
}

但让这些失败:

#define DO_X(x) for(char _do_x_var_ ## x; /*things*/)
DO_X(x){
    DO_X(x) { // <-- multiple definition of _do_x_var_x
        /*things*/
    }
}

DO_X(object->x){
    DO_X(object->x) { // <-- multiple definition of _do_x_var_object__x (or whatever)
        /*things*/
    }
}

有什么方法可以使其工作吗?也许用 __ 替换 -> 或其他什么?我找到了连接字符串的方法,但没有找到替换字符串的方法。

I have a macro where I pass in an argument and use that define a new variable based on the name of the input:

#define DO_X(x) char _do_x_var_ ## x; /* other things */

The problem is if I pass in a struct variable, it breaks:

DO_X(some_struct->thing)

becomes:

char _do_x_var_some_struct->thing; /* other things */

EDIT: What I want it to evaluate to is:

char _do_x_var_some_struct__thing; /* other things */

(or any valid variable name containing something similar to the input)

What I actually want is for these to work:

#define DO_X(x) for(char _do_x_var_ ## x; /*things*/)
DO_X(x){
    DO_X(y) {
        /*things*/
    }
}

DO_X(object->x){
    DO_X(object->y) {
        /*things*/
    }
}

but for these to fail:

#define DO_X(x) for(char _do_x_var_ ## x; /*things*/)
DO_X(x){
    DO_X(x) { // <-- multiple definition of _do_x_var_x
        /*things*/
    }
}

DO_X(object->x){
    DO_X(object->x) { // <-- multiple definition of _do_x_var_object__x (or whatever)
        /*things*/
    }
}

Is there some way to make this work? Maybe replacing -> with __ or something? I've found ways to concatenate, but not replace strings..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

‘画卷フ 2024-10-06 15:05:57

您还没有找到重写任意字符串的方法,因为宏无法做到这一点。宏名称必须是有效的标识符,而 -> 不是。 C 预处理器的功能非常有限。您可以研究 m4 以获得更强大的预处理器,但您可能走错了路。

You haven't found a way to rewrite arbitrary strings because macros cannot do that. Macros names have to be valid identifiers, which -> is not. The C preprocessor is very limited in what it can do. You could look into m4 for a stronger preprocessor, but you're likely headed down the wrong road.

呆° 2024-10-06 15:05:57

我不知道有任何预处理器机制可以将 struct->element 参数视为两个单独的标记或自动转换 ->element 。到下划线。我的建议是有一个单独的宏,例如 DO_X2(struct_ptr, element),它将添加“->”或在需要的地方使用“_”。然后您可以根据需要使用 DO_X 或 DO_X2。

如果您打算按照指示使用这些宏,则会出现一个单独的问题。内部 for 循环可以声明完全相同的变量名,并且不会被视为错误,因为它们具有不同的范围。例如,假设您的 C 编译器支持在 for 语句中声明迭代器,如下所示(我不认为这是 C 的标准行为):

for (int i=0; i<10; ++i)

那么您可以执行以下操作,并且不会被视为错误:

int sum = 0;
for (int i=0; i<10; ++i)
    for (int i=0; i<10; ++i)
        ++sum;

两个“int i" 具有不同的范围,因此它应该可以正常编译和执行。

I don't know of any preprocessor mechanism to treat a struct->element parameter as two separate tokens or to autoconvert the -> to an underscore. My suggestion is to have a separate macro, e.g. DO_X2(struct_ptr, element), which would add the "->" or "_" where needed. Then you could use DO_X or DO_X2 as appropriate.

There is a separate problem if you plan to use these macros as indicated. The inner for-loop can declare the exact same variable name and it will not be considered an error since they have different scope. For example, assuming your C compiler supports declaring the iterator within the for statement like this (which I don't believe is standard behavior for C):

for (int i=0; i<10; ++i)

Then you could do the following and it would not be considered an error:

int sum = 0;
for (int i=0; i<10; ++i)
    for (int i=0; i<10; ++i)
        ++sum;

The two "int i" have different scope so it should compile and execute just fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文