Placing a preprocessor directive inside a string literal? [duplicate]

有些话、适合烂在心里 提交于 2019-12-08 04:50:07

问题


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"

To give me Y as "/var/stuff/stuff.txt". Is this even possible to do? Thanks beforehand.

EDIT: Alternatively, would something like this concat the two literals into one?

#define X "/var/stuff"
#define Y X"/stuff.txt"

回答1:


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;
}


来源:https://stackoverflow.com/questions/14721007/placing-a-preprocessor-directive-inside-a-string-literal

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!