I have an old code that uses something very similar to that str_constdescribed here and here to perform some operations with the constexpr string. str_constis a literal type described by Scott Sharr that can be built from a string literal because it has a template constructor from const char (&)[].
Now I also have new code using boost::hana.
I would like to be able to take hana::stringand create str_constone that refers to it. The easiest way to do this is to convert hana::stringto constexpr const char (&)[]. (Actually, at this point, which is not the easiest way, the easiest way is to add a new template constructor to my implementation str_const. But at this point, the question has taken its own life, and I'm mostly interested in whether this can be done with help hana::string. So let's say I'm not allowed to change the implementation str_const.)
However, in hana docs there is a way to convert hana::stringto a run-time string hana::to<const char *>.
Optimistically, I tried various forms hana::to<const char (&)[hana::length(...)]> (...), but this leads to a failure of static statements in hana.Another option suggested by hanadocs is to use hana::unpackand then insert the characters into the array yourself. 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 it almost works, at least it compiles. But if the links are used at run time, it fails with linker: undefined reference to ... string_keeper<boost::hana::string<(char)97> >::my_array.
(Actually, I think I understand why the problem is ODR, and if I think about it a little longer, I can remember how to fix it ... not sure ...)
, . hana hana::string constexpr const char *, , . , , const char * (&)[], , , constexpr. , hana , , , - .
? hana, ? - ?