错误:粘贴“."和“红色"表示没有给出有效的预处理令牌 [英] error: pasting "." and "red" does not give a valid preprocessing token

查看:252
本文介绍了错误:粘贴“."和“红色"表示没有给出有效的预处理令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在实现X宏,但是我有一个简单的宏扩展问题.通过包括在 this 文章中,此宏(请参阅下文)用于几个宏用法示例. 编译器给出了一条错误消息,但是通过在GCC编译器中使用-E标志,我可以看到有效的C代码.

I'm implementing The X macro, but I have a problem with a simple macro expansion. This macro (see below) is used into several macros usage examples, by including in this article. The compiler gives an error message, but I can see valid C code by using -E flag with the GCC compiler.

宏X列表的定义如下:

#define LIST \
  X(red, "red") \
  X(blue, "blue") \
  X(yellow, "yellow")

然后:

#define X(a, b) foo.##a = -1;
  LIST;
#undef X

但是gcc给出了以下错误消息:

But the gcc given the following errors messages:

lixo.c:42:1: error: pasting "." and "red" does not give a valid preprocessing token
lixo.c:42:1: error: pasting "." and "blue" does not give a valid preprocessing token
lixo.c:42:1: error: pasting "." and "yellow" does not give a valid preprocessing token

就像我说的那样,通过在gcc上使用-E开关,可以看到有效的C代码:

Like I said, I can seen valid C code by using -E switch on gcc:

lixo.c:42:1: error: pasting "." and "red" does not give a valid preprocessing token
lixo.c:42:1: error: pasting "." and "blue" does not give a valid preprocessing token
lixo.c:42:1: error: pasting "." and "yellow" does not give a valid preprocessing token
  foo.red = -1; foo.blue = -1; foo.yellow = -1;;

什么是有效的预处理令牌?有人可以解释吗?

What's a valid preprocessing token? Can someone explain this?

(在您说为什么不只是初始化还是memset()?"之前,这不是我的真实代码.)

(before you say "why not just an either initialize or memset()?" it's not my real code.)

推荐答案

.分隔标记,因此您不能使用##,因为.red不是有效的标记.仅在将两个令牌串联到一个令牌中的情况下,才使用##.

. separates tokens and so you can't use ## as .red is not a valid token. You would only use ## if you were concatenating two tokens into a single one.

这有效:

#define X(a, b) foo.a = -1;

什么是有效的预处理令牌?有人可以解释吗?

What's a valid proprocessing token? Can someone explain this?

这就是被解析/词法化的东西. foo.bar将被解析为3个令牌(两个标识符和一个运算符):foo . bar如果使用##,您将仅获得2个令牌(一个标识符和一个无效令牌):foo .bar

It is what gets parsed/lexed. foo.bar would be parsed as 3 tokens (two identifiers and an operator): foo . bar If you use ## you would get only 2 tokens (one identifier and one invalid token): foo .bar

这篇关于错误:粘贴“."和“红色"表示没有给出有效的预处理令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆