将预处理器指令放在字符串文字中? [英] Placing a preprocessor directive inside a string literal?
问题描述
我正在尝试使用 #define
定义字符串文字(在这种情况下为文件路径),但是我无法从另一个#define'd元素获取路径。简而言之,我想要这样的东西:
I'm trying to use #define
to define a string literal (a filepath in this case), but I cannot get the path FROM another #define'd element. In short, I'd like something like this:
#define X /var/stuff
#define Y "X/stuff.txt"
给我 Y
为 / var / stuff / stuff.txt
。这有可能吗?
To give me Y
as "/var/stuff/stuff.txt"
. Is this even possible to do? Thanks beforehand.
编辑:或者,这样的东西会将两个文字合并为一个吗?
Alternatively, would something like this concat the two literals into one?
#define X "/var/stuff"
#define Y X"/stuff.txt"
推荐答案
不是您所拥有的方法,不是-字符串文字内不会发生宏替换。
Not the way you have it, no -- macro replacement does not happen inside string literals.
但是,您可以将片段中的字符串粘贴在一起:
You can, however, paste together a string from pieces:
#define str(x) #x
#define expand(x) str(x)
#define X /var/stuff
#define Y expand(X) "/stuff.txt"
此示例的快速演示代码:
Quick demo code for this:
#include <iostream>
int main(){
std::cout << Y;
}
这篇关于将预处理器指令放在字符串文字中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!