6

我正在尝试使用 Boosthana::transform来更改hana::tuple. 例如,说我有

constexpr auto some_tuple = hana::tuple_t<int, char *, bool>;

我想生产

constexpr auto transformed_tuple = hana::tuple_t<std::vector<int>,
                                                 std::vector<char *>, 
                                                 std::vector<bool>>;

尝试 1

解决方案对我来说似乎很简单:使用hana::transform并让应用的函数返回hana::type_c<std::vector<decltype(T)::type>>。但是,我无法完成这项工作:

constexpr auto transformed_tuple = hana::transform(some_tuple, [](auto T) {
    using inner_type = typename decltype(T)::type;
    return hana::type_c<std::vector<inner_type>>;
});

问题是 lambda 表达式不是constexpr- 并且我想留在 C++14 中,即 lambda 不能是constexpr.

尝试 2

我的下一个想法:如果我将它包裹hana::transform成一个decltype,然后在上面使用hana::type_c呢?这样,lambda 永远不需要被评估(只需要推断它的返回类型),并且constexprness 应该无关紧要:

constexpr auto transformed_tuple = 
    hana::type_c<decltype(hana::transform(some_tuple, [](auto T) {
        using inner_type = typename decltype(T)::type;
        return hana::type_c<std::vector<inner_type>>;
    }))>;

但是,现在我遇到了 lambda 表达式可能不会出现在“未评估的上下文”中的问题。

我的方法完全错误吗?我应该使用其他东西hana::transform吗?

谢谢你的帮助。

编辑

示例代码:

#include <boost/hana.hpp>
#include <boost/hana/tuple.hpp>
#include <boost/hana/type.hpp>
namespace hana = boost::hana;

#include <vector>

constexpr auto some_tuple = hana::tuple_t<int, char *, bool>;

/** What I want:
 *
 *   constexpr auto transformed_tuple 
 *       = hana::tuple_t<std::vector<int>,
 *           std::vector<char *>, 
 *           std::vector<bool>>;
**/

#if ATTEMPT1
    constexpr auto transformed_tuple = hana::transform(some_tuple, [](auto T) {
        using inner_type = typename decltype(T)::type;
        return hana::type_c<std::vector<inner_type>>;
    });

#elif ATTEMPT2
    constexpr auto transformed_tuple = hana::type_c<decltype(hana::transform(some_tuple, [](auto T) {
        using inner_type = typename decltype(T)::type;
        return hana::type_c<std::vector<inner_type>>;
    }))>;

#endif
4

1 回答 1

8

Boost.Hanahana::template_用于将类型应用于返回类型的模板。

#include <boost/hana/assert.hpp>
#include <boost/hana/equal.hpp>
#include <boost/hana/transform.hpp>
#include <boost/hana/tuple.hpp>
#include <boost/hana/type.hpp>
#include <vector>

namespace hana = boost::hana;


int main() {
    constexpr auto some_tuple = hana::tuple_t<int, char *, bool>;
    constexpr auto expected_tuple = hana::tuple_t<std::vector<int>,
                                                  std::vector<char*>,
                                                  std::vector<bool>>;

    constexpr auto transformed_tuple = hana::transform(some_tuple, hana::template_<std::vector>);

    BOOST_HANA_CONSTANT_CHECK(transformed_tuple == expected_tuple);
}
于 2018-04-11T16:07:02.623 回答