1

Is there a C++ way of concatenating two constexpr C-strings at compile time? I know how to do it in C with defines, but would really rather have the scope reduction and explicit type system of C++. My primary goal is to have a nice way of concatenating strings at compile time in one line.

Here's an example of what does work in the way I don't want:

#define STR1 "foo"
#define STR2 "blah"
#define CONCATED STR1 STR2

Here's an example of what doesn't work in the way I do want:

constexpr const char *str1 = "foo";
constexpr const char *str2 = "blah";
constexpr const char *concated = str1 str2;
mfdeakin
  • 73
  • 5
  • This answer seems to be in the right direction: https://stackoverflow.com/questions/2978259/programmatically-create-static-arrays-at-compile-time-in-c My template foo doesn't seem to be up to snuff, though. My attempt to use constexpr functions has not led to the answer yet, either. – mfdeakin Aug 21 '15 at 20:23

1 Answers1

1

It can only be done at compile time with string literals.

 constexpr const char concatenated[] = "foo" "blah";

This is the effect achieved by your CONCATED macro (in fact, it is literally how the preprocessor expands usage of that macro).

If you want to concatenate named variables, then it is not possible to satisfy your requirement(s) of doing concatenation at compile time avoiding use of heap or stack.

The two obvious choices are to allocate enough memory to hold the result and then use strcat(), or to use the std::string type. Both of these involve using heap or stack (depending on how you allocate memory, and how the allocator you choose for std::string works) and both do the concatenation at run time.

Peter
  • 35,646
  • 4
  • 32
  • 74
  • 1
    It's NOT compiler dependent. `& "foo" "blah"` is guaranteed to remain valid for the life of your program, which precludes stack allocation. – MSalters Aug 21 '15 at 08:24
  • You're mixing concepts, MSalters. The notion of stack, itself, has nothing to do with lifetime of a string literal. – Peter Aug 21 '15 at 11:48
  • 1
    I agree with that comment. But you said in the answer that the string literal `"foo" "blah"` may use the stack, depending on the compiler. That part of the answer is incorrect. The lifetime of a stack frame is only the duration of a function invocation, but the lifetime of a string literal is the whole duration of the process. – MSalters Aug 21 '15 at 12:58
  • You're misinterpreting completely, MSalters. In any event, I'm removing the sentence completely, since you won't let it go, and are just making irrelevant comments. – Peter Aug 21 '15 at 13:10
  • No template magicks to save me the effort? No hidden C++11 features? I'm reduced to poorly typed and scoped C methods? I was thinking something like this might work, with issues with the null terminators, but haven't figured it out yet: `constexpr const char str1[] = "foo"; constexpr const char str2[] = "blah"; template struct constconcat { static constexpr const char p1[sizeof(s1)] = s1; static constexpr const char p2[sizeof(s2)] = s2; };` – mfdeakin Aug 21 '15 at 16:56
  • C strings are a feature with a number of anomalies inherited from (well, you know) C. – Peter Aug 21 '15 at 22:12