I have some old code that uses something very similar to str_const described here and here to do some constexpr string manipulation. str_const is a literal type described by Scott Schurr that can be constructed from a string literal, because it has a template constructor from const char (&)[].
I now also have some new code using boost::hana.
I would like to be able to take a hana::string and create a str_const that refers to it. The simplest way to do this is to convert a hana::string to a constexpr const char (&)[]. (Actually, at this point that's not the simplest way, the simplest way is surely to add a new template constructor to my str_const implementation. But at this point the question has taken on a life of it's own and I'm mainly interested in whether this can be done with hana::string. So let's assume that I'm not allowed to change the str_const implementation.)
However, in hana docs the way to convert hana::string to a run-time string is hana::to<const char *>.
Optimistically, I tried various forms of hana::to<const char (&)[hana::length(...)]> (...) but this causes static assertions in hana to fail.
The other option suggested by hana docs is to use hana::unpack and then stick the characters in an array myself. I wrote this code
template <typename T, size_t N>
struct array {
T arr[N];
};
struct char_packer {
template <typename... Ts>
constexpr auto operator()(Ts... ts) -> array<const char, sizeof...(ts) + 1> {
return array<const char, sizeof...(ts) + 1>{{ ts... , 0 }};
}
};
template <typename S>
struct string_keeper {
static constexpr auto my_array = hana::unpack(S{}, char_packer{});
};
template <int N>
using char_arr = const char [N];
template <typename S>
constexpr auto to_string_literal(S &&) -> const char_arr<decltype(hana::length(S{}))::value + 1> & {
return string_keeper<S>::my_array.arr;
}
I think this almost works, at least it compiles. But if the references are also used at run-time then it fails with a linker error: undefined reference to ... string_keeper<boost::hana::string<(char)97> >::my_array.
(Actually I think I understand why that's an ODR problem and if I think on it a bit longer I might remember how to fix it... not sure...)
Intuitively, I feel that there must be a way to do this. Because, hana already allows me to convert hana::string to constexpr const char * where the pointer points to exactly the array I want. In fact it even suggests that there's might be an evil option where I try to coerce the const char * back to (&)[] type, although that also seems like it would require doing things that won't be allowed in constexpr functions. Anyways if hana can make that array then surely I can too, or somehow convince it to give it to me more exactly.
Is there a way to fix my code above? Is there an easier way to do this within hana that I overlooked? Is this actually impossible for some reason?