Convert `hana :: string` to` constexpr const char (&) []`

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, ? - ?

+4
1

, char . str_const , , , str_const .

, hana::string:

#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', "");
}
+1

Source: https://habr.com/ru/post/1682273/


All Articles