In C++11 there is no way to store temporary string anywhere at compile time. So I can suggest you this approach:
(It is fast-made sketch, but it is well descriptive)
#include <stdio.h>
template <char...>
struct StringTuple;
template <char TargHead>
struct StringTuple<TargHead> {
static constexpr char kSymbol = TargHead;
static void print() {
printf(kSymbol ? "%c\n" : "\n", kSymbol);
}
};
template <char TargHead, char... TargTail>
struct StringTuple<TargHead, TargTail...> {
using Next = StringTuple<TargTail...>;
static constexpr char kSymbol = TargHead;
static void print() {
if (kSymbol) {
printf("%c", kSymbol);
Next::print();
} else {
printf("\n");
}
}
};
constexpr int length(char *string) {
return (string[0] == 0) ? 1 : (length(string + 1) + 1);
}
constexpr char get(char *string, int i) {
return i < length(string) ? string[i] : 0;
}
#define ST(string) \
StringTuple< \
get(string, 0), \
get(string, 1), \
get(string, 2), \
get(string, 3), \
get(string, 4), \
get(string, 5), \
get(string, 6), \
get(string, 7), \
get(string, 8), \
get(string, 9), \
get(string, 10), \
get(string, 11), \
get(string, 12), \
get(string, 13), \
get(string, 14), \
get(string, 15), \
get(string, 16), \
get(string, 17), \
get(string, 18), \
get(string, 19), \
get(string, 20), \
get(string, 21), \
get(string, 22), \
get(string, 23), \
get(string, 24), \
get(string, 25), \
get(string, 26), \
get(string, 27), \
get(string, 28), \
get(string, 29), \
get(string, 30), \
get(string, 31), \
get(string, 32), \
get(string, 33), \
get(string, 34), \
get(string, 35), \
get(string, 36), \
get(string, 37), \
get(string, 38), \
get(string, 39), \
get(string, 40), \
get(string, 41), \
get(string, 42) \
>
int main() {
ST("Hello, compile-time world!")::print();
}
Bash code to generate a part of macro:
for i in `seq 0 42`; do echo " get(string, $i), \\"; done
You have to pass large number (1000 or more) to this generator to support all your strings and you have to make static assertion if string exceeds this limit.
I use such generated macros in my own scientific projects. I know it seems messy, but it works. Example of usage of generated macro:
#define PRINT(a) print(a);
FOREACH_MACRO(PRINT, a, b, c) // print(a);print(b);print(c);
I will try find more pretty solution, but at first I will use this.