Используйте hana::transform для преобразования типов внутри кортежа в C++14
Я пытаюсь использовать Boost's hana::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>>;
});
Это проблема в том, что лямбда-выражение не является constexpr
- и я хочу остаться в C++14, то есть лямбда не может быть constexpr
,
Попытка 2
Моя следующая мысль: что если я заверну hana::transform
в decltype
, а затем использовать hana::type_c
на что? Таким образом, лямбда не нуждается в оценке (должен быть выведен только ее тип возврата), и constexpr
Несс не должен иметь значения:
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>>;
}))>;
Однако теперь я сталкиваюсь с проблемой, что лямбда-выражения могут не появляться в "неоцененном контексте".
Мой подход совершенно неверный? Должен ли я использовать что-то еще, чем 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
1 ответ
Boost.Hana имеет hana::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);
}