如何反转hana :: string

时间:2018-12-14 17:17:28

标签: c++ templates boost-hana

有人知道如何逆转boost :: hana :: string吗?

以下内容无效:

#include <boost/hana.hpp>
#include <boost/hana/reverse.hpp>
auto s = BOOST_HANA_STRING("abc");
auto s2 = boost::hana::reverse(s);

,并显示错误消息:

boost/hana/reverse.hpp:36:9: error: static_assert failed due to requirement 
'hana::Sequence<S>::value' "hana::reverse(xs) 
 requires 'xs' to be a Sequence"

hana string doc说:

  

尤其是,模板参数是char不应被认为是理所当然的。以字符常量形式访问hana :: string内容的正确方法是使用hana :: unpack,.c_str()或hana :: to

所以,我想看的方向是“如何将hana :: string转换为Sequence

谢谢!

2 个答案:

答案 0 :(得分:1)

哼,我不得不深入研究一下汉娜琴弦的内部。

但是Hana具有很好的功能,因此这个简短的解决方案就足够了:

auto reverse_boost_hana_string = [](auto s) {
    auto add_reverse = [=](auto xs, auto ys) {
        auto ys_s = boost::hana::string<ys>(); // this is what I had wrong for a while
        return ys_s + xs;
    };
    auto reversed = boost::hana::fold_left(s, BOOST_HANA_STRING(""), add_reverse);
    return reversed;
};


int main()
{
    auto s = BOOST_HANA_STRING("abcdef");
    auto s2 = reverse_boost_hana_string(s);
    std::cout << "Reversed ==>" <<  s2.c_str() << "<==\n";
}

在没有模板干扰的情况下编写TMP非常令人高兴。

答案 1 :(得分:1)

Boost.Hana确实具有功能,但是如果您可以避免在类型上折叠,则可以获得更编译时更有效的算法。

#include <array>
#include <boost/hana.hpp>

namespace hana = boost::hana;

template <typename S, unsigned long ...i>
auto reverse_string_impl(S s, std::index_sequence<i...>) {
  constexpr unsigned long n = sizeof...(i);
  constexpr char const* c = hana::to<char const*>(s);
  return hana::make_string(hana::char_c<c[n - i - 1]>...);
  // would be better but assumes stuff about the impl of hana::string
  //return hana::string<c[n - i - 1]...>{};
}

template <typename S>
constexpr auto reverse_string(S) {
  return reverse_string_impl(S{},
    std::make_index_sequence<hana::length(S{})>{});
}

int main() {
  BOOST_HANA_CONSTANT_ASSERT(
      BOOST_HANA_STRING("foo") == reverse_string(BOOST_HANA_STRING("oof"))
  );

  // or just convert to a tuple and back (less efficient)
  BOOST_HANA_CONSTANT_ASSERT(
      BOOST_HANA_STRING("foo") ==
        hana::unpack(
          hana::reverse(hana::unpack(BOOST_HANA_STRING("oof"),
                        hana::make_tuple)),
          hana::make_string)


  );
}