It happened to many people, and it happened to me. I am stuck playing with compile-time strings in C ++.
I decided to use an unacceptable approach: using the template <char...> classes.
This is what I came up with, is very common, nothing special, and also does not work.
template <char... chars> class string { public: static constexpr const char value[] = {chars...}; constexpr string() { } constexpr operator decltype(value) & () const { return value; } }; template <char... chars> constexpr const char string <chars...> :: value[];
My idea was to create a string instance of constexpr constructive and expose some kind of constexpr so that it would provide its contents.
Now if i do
static constexpr const char x[] = "ciao"; template <const char * str> void print() { std :: cout << str << std :: endl; } print <x> ();
It works and says ciao . I get ciao as well if I do
std :: cout << string <'c', 'i', 'a', 'o'> {} << std :: endl;
or
print <string <'c', 'i', 'a', 'o', '\0'> :: value> ();
But when I do
print <string <'c', 'i', 'a', 'o', '\0'> {}> ();
I get: No matching function for call to print .
I definitely missed something. Is it possible to do what I'm trying to do? Creating an instance does constexpr to somehow return a value ? If this worked, I could easily do operators and string manipulations at compile time, the "only" drawback is the ultra-boring 'i', 'n', 'i', 't', 'i', 'a', 'l', 'i', 'z', 'a', 't', 'i', 'o', 'n' .
Further experiments
I did another experiment that works great.
template <char... chars> class string { public: constexpr string() { } constexpr operator size_t () const { return sizeof...(chars); } }; template <size_t length> void print() { std :: cout << length << std :: endl; } print <string <'c', 'i', 'a', 'o'> {}> ();
And he prints pretty 4 .