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,438
  • 68
  • 55
  • 126
SuperTron
  • 4,093
  • 6
  • 33
  • 61

1 Answers1

3

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
  • 455,417
  • 76
  • 598
  • 1,067