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?
One other problem is that, when returned from a function, a raw char array will be decayed to a pointer. I would suggest constructing the str_const
object in the context of your function which I believe fulfills your intent of creating str_const
without changing its interface.
The following example uses a top level variable template to create the array which is what the hana::string
implementation uses:
#define BOOST_HANA_CONFIG_ENABLE_STRING_UDL
#include <boost/hana.hpp>
#include <stdexcept>
namespace hana = boost::hana;
using namespace hana::literals;
class str_const {
const char * const p_;
const std::size_t sz_;
public:
template <std::size_t N>
constexpr str_const( const char( & a )[ N ] )
: p_( a ), sz_( N - 1 ) {}
constexpr char operator[]( std::size_t n ) const {
return n < sz_ ? p_[ n ] : throw std::out_of_range( "" );
}
constexpr std::size_t size() const { return sz_; }
};
template <char ...c>
constexpr char string_storage[sizeof...(c) + 1] = {c..., '\0'};
struct to_str_const_helper {
template <typename ...Ts>
constexpr auto operator()(Ts...) {
return str_const(string_storage<Ts::value...>);
}
};
template <typename S>
constexpr auto to_str_const(S) {
return hana::unpack(S{}, to_str_const_helper{});
}
int main()
{
constexpr str_const str = to_str_const("foo"_s);
static_assert(str[0] == 'f', "");
static_assert(str[1] == 'o', "");
static_assert(str[2] == 'o', "");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With