Convert constexpr struct to runtime

I am trying to use a template class (here Foo), with a base type like:

  • hana::tuple<hana::pair<hana::type<int>, Runtime>>with a Runtimeclass that, oddly enough, cannot be constepxr.

But a type can be built in several ways: why I use:

  • hana::tuple<hana::pair<hana::type<int>, hana::type<Runtime>>> do work at compile time.

So, the question basically is how to convert from the first type of tuple to the second . I wonder if there is anything in hanathat that could help me. Or better yet, some tips on such a "conversion."

namespace hana = boost::hana;
using namespace hana::literals;

struct Runtime { std::vector<int> data; };

template < typename T >
struct Foo {
  T data;
};

constexpr decltype(auto)  convertMap(auto storageMap) {

return hana::make_type(hana::transform(
    storageMap,
    [] (auto pair) {
      return hana::make_pair(
        hana::first(pair),
        typename decltype(hana::typeid_(hana::second(pair)))::type {});
    }));
}

int main() {

  constexpr auto map = hana::make_tuple(
      hana::make_pair(hana::type_c<int>, hana::type_c<Runtime>)
      );

  constexpr auto result = convertMap(map);
  static_assert(result ==
     hana::type_c<hana::tuple<hana::pair<hana::type<int>, Runtime>>>);

  Foo<typename decltype(result)::type> test;
}

, ++ 1z convertMap hana::transform lambdas, constexpr, hana::make_type hana::type_c.

test.cpp: In function ‘int main()’:
test.cpp:70:41: error: ‘constexpr decltype(auto) convertMap(auto:27) [with auto:27 = boost::hana::tuple<boost::hana::pair<boost::hana::type_impl<int>::_, boost::hana::type_impl<Runtime>::_> >]’ called in a constant expression
   constexpr auto result = convertMap(map);
                                         ^
test.cpp:53:27: note: ‘constexpr decltype(auto) convertMap(auto:27) [with auto:27 = boost::hana::tuple<boost::hana::pair<boost::hana::type_impl<int>::_, boost::hana::type_impl<Runtime>::_> >]’ is not usable as a constexpr function because:
 constexpr decltype(auto)  convertMap(auto storageMap) {
                           ^~~~~~~~~~
test.cpp:53:27: error: temporary of non-literal type ‘boost::hana::tuple<boost::hana::pair<boost::hana::type_impl<int>::_, Runtime> >’ in a constant expression
In file included from /usr/include/boost/hana/detail/struct_macros.hpp:29:0,
                 from /usr/include/boost/hana/adapt_adt.hpp:15,
                 from lib/hana/include/boost/hana.hpp:59,
                 from test.cpp:1:
/usr/include/boost/hana/tuple.hpp:68:12: note: ‘boost::hana::tuple<boost::hana::pair<boost::hana::type_impl<int>::_, Runtime> >’ is not literal because:
     struct tuple
            ^~~~~
/usr/include/boost/hana/tuple.hpp:68:12: note:   ‘boost::hana::tuple<boost::hana::pair<boost::hana::type_impl<int>::_, Runtime> >’ has a non-trivial destructor
+4
1

, , - constexpr, decltype(...){} "force constexpr":

template <typename T>
decltype(auto) convertMapImpl(T storageMap)
{
    return hana::make_type(hana::transform(storageMap, [](auto pair) {
        return hana::make_pair(hana::first(pair),
            typename decltype(hana::typeid_(hana::second(pair)))::type{});
    }));
}

template <typename T>
constexpr decltype(auto) convertMap(T storageMap)
{
    return decltype(convertMapImpl(storageMap)){};
}

live wandbox


, auto - gcc - , .

+6

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


All Articles