1

I found a lot of questions and answers concatenating strings with the C or C++ preprocessor; for instance this question (but there are many more).

What I couldn't find was whether it was possible to concatenate to the same string. To be more clear, something like this

#define MY_STRING "Hello"
#define MY_STRING MY_STRING " world"
// Now MY_STRING is "Hello world"

If I had to write it during "runtime", I would write something like

char my_string[80];
strcpy(my_string, "Hello");

strcat(my_string, " world"); // <- similar to this operation, but in preprocessor

Please note, however, that this is not what I'm trying to do; I want the concatenation to be performed at compile time.

Is this possible? Or a define is "immutable"?

This question is not about a particular flavor of C or C++; if this can be implemented in only one of the two languages or only with some particular compiler please specify it in the answer

EDIT: as Lightness Races in Orbit partially guessed, the main point of my question revolves around conditional compilation and, moreover, expandability.

As for conditional compilation, what I currently do is

#if COND_1
    #define STR_COND_1 " val1"
#else
    #define STR_COND_1 ""
#endif
#if COND_2
    #define STR_COND_2 " val2"
#else
    #define STR_COND_2 ""
#endif

#define STR STR_COND_1 STR_COND_2

The problem here is that this can lead to errors when the conditions become too many (it's easy to forget one), while a concatenation does not have this problem.

As for expandability, I mean that if I have to add another module which adds its string to the STR one (for instance, COND_3), I have to manually add it to the STR definition, while with a concatenation it is automatic.

Now, these examples are really easy, so forgetting it is difficult, but when you have a project where these things are scattered around in lots of files forgetting one variable is easy and can lead to a lot of time wasted

frarugi87
  • 2,826
  • 1
  • 20
  • 41
  • 3
    Just use `const std::string s = "Hello World"` and move on to catching bigger and better fishes. – Ron Mar 24 '18 at 15:11
  • 2
    @Ron maybe I was not clear enough, but I don't want it to be done during the program, but at compile time... and moreover can you concatenate s to other strings? – frarugi87 Mar 24 '18 at 15:14
  • 3
    @Ron: The question is valid. The world does not all revolve around dynamic allocation at runtime. The preprocessor is a useful tool and asking questions about it is acceptable. – Lightness Races in Orbit Mar 24 '18 at 15:26
  • There's a nifty technique in the second part of this answer: https://stackoverflow.com/a/46019437/1566221 – rici Mar 24 '18 at 15:39

2 Answers2

2

You can't redefine a preprocessor macro. So, no.

If you don't mind doing it all in one go, two literals can be concatenated so:

#define MY_STRING "hello" " world"

Or use different names. Here's an example, introducing a conditional as I expect that is the real crux of your problem that you have omitted from the question:

#define MY_STRING_BASE "hello"

#ifdef FOO
#define MY_STRING MY_STRING_BASE " world"
#else
#define MY_STRING MY_STRING_BASE
#endif
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
0

Macros cannot be redefined. Any build-in solution that fully suits your need does not come to my mind but what about usage of simple regex in order to generate list of all conditions?

Assuming that each condition macro starts with STR_COND_ following regex run over all files shall produce the list of all conditions:

(STR_COND_\w+)
apr
  • 370
  • 1
  • 5