使用预处理器将JSON作为字符串嵌入C ++代码中 [英] Embedding JSON as a string in C++ code using preprocessor

查看:78
本文介绍了使用预处理器将JSON作为字符串嵌入C ++代码中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Chromium项目中看到了C ++和JSON代码的混合体。

I saw a mix of C++ and JSON code in the Chromium project.

例如在此文件中:
config / software_rendering_list_json.cc

此宏是魔术吗?

#define LONG_STRING_CONST(...) #__VA_ARGS__

如何字符串化任意JSON内容?

How does it "stringify" arbitrary JSON content?

推荐答案

您猜对了!

会转换后续标记到包含该标记文本的C字符串文字中。在这种情况下,下一个标记是特殊的 __ VA_ARGS __ 宏关键字,该关键字替换为(可变)宏的所有参数,该宏对应于源代码中的JSON。

# inside a macro body turns the subsequent token into a C string literal containing that token's text. In this case, the next token is the special __VA_ARGS__ macro keyword that is substituted with all the arguments to the (variadic) macro, which corresponds to the JSON in the source code.

这篇关于使用预处理器将JSON作为字符串嵌入C ++代码中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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