问题
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