- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
假设某些标记 FOO
、BAR
和 DUD
(可能还有更多)是否 #defined
.我想要一个生成有效扩展名称的宏 EXTEND(name)
,例如
#define FOO
#undef BAR
#define DUD
EXTEND(object)
扩展为
object_foo_dud
如果有 n
个宏标记(如 FOO
、BAR
和 DUD
)?我认为 O(n) 行应该是可能的,但是怎么做呢?
我已经试过了:
#ifdef FOO
# define ExtFOO(name) name ## _foo
#else
# define ExtFOO(name) name
#endif
#ifdef BAR
# define ExtBAR(name) ExtFOO(name) ## _bar
#else
# define ExtBAR(name) ExtFOO(name)
#endif
#ifdef DUD
# define ExtDUD(name) ExtBAR(name) ## _dud
#else
# define ExtDUD(name) ExtBAR(name)
#endif
#define EXTEND(name) ExtDUD(name)
但是
test.cc:26:5: error: pasting formed ')_dud', an invalid preprocessing token
EXTEND(object)
^
最佳答案
##
运算符连接两个预处理标记,并且必须产生一个单个 有效标记。例如,来自 C99 规范的第 6.10.3.3 节:
For both object-like and function-like macro invocations, before the replacement list is reexamined for more macro names to replace, each instance of a
##
preprocessing token in the replacement list (not from an argument) is deleted and the preceding preprocessing token is concatenated with the following preprocessing token. Placemarker preprocessing tokens are handled specially: concatenation of two placemarkers results in a single placemarker preprocessing token, and concatenation of a placemarker with a non-placemarker preprocessing token results in the non-placemarker preprocessing token. If the result is not a valid preprocessing token, the behavior is undefined. The resulting token is available for further macro replacement. The order of evaluation of##
operators is unspecified.
所以扩展 ExtBAR(name) ## _dud
是无效的,因为它会产生 ExtBAR(object)_dud
。
我会采用以下方法:
#ifdef FOO
# define ValFOO _foo
#else
# define ValFOO
#endif
#ifdef BAR
# define ValBAR _bar
#else
# define ValBAR
#endif
#ifdef DUD
# define ValDUD _dud
#else
# define ValDUD
#endif
#define CONCAT(a, b, c, d) a ## b ## c ## d
#define XCONCAT(a, b, c, d) CONCAT(a, b, c, d)
#define EXTEND(name) XCONCAT(name, ValFOO, ValBAR, ValDUD)
需要中间的 XCONCAT
步骤,因为如果使用 ##
连接宏参数,则它们不会扩展。
关于c++ - 如何根据预定义的宏(C++ 预处理器)组成有效的 token ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23218925/
编辑:我将问题重新表述为更简单且不那么特定领域的问题: 在下面的代码中,我想实现 mplus 函数,该函数结合了受特定字段存在约束的两个函数。结果函数应受两个字段的存在的约束。 谢谢 ! import
我的代码有什么问题?提前致谢! 我正在尝试为我的 docker 容器设置一个虚拟主机。在本地主机上:8000 完美运行,但是当我尝试通过 http://borgesmelo.local/访问时,出现错
这个问题在这里已经有了答案: CSS technique for a horizontal line with words in the middle (34 个答案) 关闭 2 年前。 首先,这是
我是 React Js 的新手,并尝试了解如何通过 ajax 对从不同来源获得的数据进行组合(如下所述: Reactjs - loadResourcesFromServer - combine two
我一直在关注 this tutorial在 docker 服务和 swarms 上。但是我在不同 docker 容器之间的网络连接上遇到了一些问题。 以下是我的 docker-compose.yml
我正在尝试创建一个相对简单的设置来开发和测试 npm 包。事实上,一个问题是,在将代码卷安装到容器后,它会替换 node_modules。 我尝试了很多通常合乎逻辑的东西,主要是为了将 node_mo
我有四种类型A、B、C和D,初始值x Future[Option[A]] 和三个函数:f1: A => Option[B] , f2: B => Future[Option[C]] 和 f3: C =
我有一个包含单词的语料库。我想对他们做一个分析。我接受它们是土耳其字符,但其中一些不是。所以我想检查一个词是否不包含土耳其字符或数字。我的意思是: hey4 valid 33 vali
这是我正在为 Java 入门课解决的学校问题。作业是编写一个程序,生成一个由随机生成的二进制数组成的 8 x 8 矩阵,并让程序检查哪些列(如果有)全为 0,以及主对角线和次对角线是否也由零组成。主对
我正在尝试编写一个由任意数量的 lambda 函数组成的函数。 我有两个简单的 lambda 函数。 f = lambda x: x + 1 g = lambda x: x**2 我对组合函数的尝试是
我在这里学习 Docker Compose 教程 https://docs.docker.com/get-started/part5/#recap-optional version: "3" serv
我正处于要为应用程序编写 androidTests 的状态。 阅读 Testing Compose 的文档,我创建了一个文件并编写了一个简单的测试来检查进度: ExamineTest.kt: clas
我最近刚跨过木偶继承。围绕它的几个问题: 使用p继承是一种好习惯吗?一些经验丰富的木偶同事告诉我,木偶的继承不是很好,我不太相信。 来自OO世界,我真的很想了解木偶继承的原理,以及覆盖的原理。 最佳答
我正在尝试使用 docker-compose 创建一个基本网页 这是我的 yml 文件 identidock: build: . ports: - "5000:5000" envir
我们有docker-compose.yml,其中包含Kafka,zookeeper和schema registry的配置 当我们启动docker compose时,出现以下错误 docker-comp
我在玩 Haskell 的类型时无意中发现了 length 。总和 有效。是否有一些语义应该允许它工作,或者这仅仅是类型定义的一个缺点?我在下面编写了每个类型定义。 length :: Foldabl
如何防止缩小仅包含 JFormattedTextFields 的 JPanel。 JFormattedTextField textF1; JFormattedTextField textF2;
我尝试更 retrofit 载我的 Docker 卷的用户,但执行此操作的是“root”用户,而不是“安全用户”。知道如果我在 Dockerfile 中执行“chown -R/var/www”,这将不
我正在尝试将 scalaz 的 ioeffect IO[E,A] monad 用于一段非常有效的代码。 我试图用 IO[E,A] 在高层次上重写的代码需要一些关于存储在云中的文件的元数据。代码试图:
这个问题已经有答案了: What special characters must be escaped in regular expressions? (13 个回答) 已关闭 5 年前。 java中
我是一名优秀的程序员,十分优秀!