2

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"
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
SuperTron
  • 4,203
  • 6
  • 35
  • 62

1 Answers1

4

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;
}
Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111