boost-hana

Boost.Hana: How to check if function has specialisation for a certain type?

情到浓时终转凉″ 提交于 2019-12-04 18:10:31
I have a template function that has no definition by default but it specialised by some types: template <typename T> auto foo(bar &, const T &) -> void; template <> auto foo<std::string>(bar &, const std::string &) -> void {} How do I write a constexpr function that tells me if type T has a specialisation for the above function? My best effort: namespace detail { auto has_foo(hana::is_valid([](auto &b, const auto &t) -> decltype(foo(b, t)) {})); } // namespace detail template <typename T> constexpr auto has_foo() -> bool { using hana::type_c; return detail::has_foo(type_c<bar>, type_c<T>); }

Going from hana::tuple_t to hana::tuple

非 Y 不嫁゛ 提交于 2019-12-04 14:08:57
I have a hana::tuple_t<int, char, double, float> , and I want to use this to create a hana::tuple<int, char, double, float> . I thought that using hana::to<hana::tuple_tag> would transform the hana::tuple_t<int, char, double, float> into a hana::tuple<int, char, double, float> ; but that is not the case since the following always fails: auto oType = hana::tuple_t<int, char, double, float>; BOOST_HANA_CONSTANT_ASSERT( hana::to<hana::tuple_tag>(oType) == hana::make_tuple(1, 'C', 1.0, 1.0f) ); I've also tried using hana::transform , but with no luck (although I suspect I'm doing it wrong): auto

Boost hana get index of first matching

耗尽温柔 提交于 2019-12-01 23:41:57
So I am trying to make a library using boost::hana that requires the functionality to get the index of a element based on the value: constexpr auto tup = boost::hana::make_tuple(3_c, boost::hana::type_c<bool>); auto index = get_index_of_first_matching(tup, boost::hana::type_c<bool>); // ^^^^^ would be a boost::hana::int_<1> Is there a possible way to do this? Better yet, is it already in hana and I don't know about it? Thanks for the support! Hana does not provide an algorithm to do this out-of-the-box. If it seems like a much desired feature, I could add such an algorithm fairly easily. It

Why is this nested lambda not considered constexpr?

一个人想着一个人 提交于 2019-12-01 01:49:00
问题 I'm trying to create a curried interface using nested constexpr lambdas, but the compiler does not consider it to be a constant expression. namespace hana = boost::hana; using namespace hana::literals; struct C1 {}; template < typename T, std::size_t size > struct Array {}; constexpr auto array_ = [] (auto size) { return [=] (auto type) { return hana::type_c<Array<typename decltype(type)::type, size()>>; }; }; int main() { constexpr auto c1 = hana::type_c<C1>; constexpr auto test = hana::type

hana::second can't deduce type

只愿长相守 提交于 2019-11-29 15:21:55
I'm trying to access a hana::type from a pair using hana::second ... namespace hana = boost::hana; using namespace hana::literals; struct Key {}; struct Foo {}; int main() { auto test = hana::make_tuple( hana::make_pair( hana::type_c<Key>, hana::type_c<Foo>)); typename decltype(hana::type_c<Foo>)::type finalTest; //Ok typename decltype(hana::second(test[0_c]))::type finalTest2; //Error } But I am getting the following compiler error: stacktest.cpp: In function ‘int main()’: stacktest.cpp:17:12: error: decltype evaluates to ‘boost::hana::type_impl<Foo>::_&’, which is not a class or enumeration

hana::second can't deduce type

混江龙づ霸主 提交于 2019-11-28 08:46:19
问题 I'm trying to access a hana::type from a pair using hana::second ... namespace hana = boost::hana; using namespace hana::literals; struct Key {}; struct Foo {}; int main() { auto test = hana::make_tuple( hana::make_pair( hana::type_c<Key>, hana::type_c<Foo>)); typename decltype(hana::type_c<Foo>)::type finalTest; //Ok typename decltype(hana::second(test[0_c]))::type finalTest2; //Error } But I am getting the following compiler error: stacktest.cpp: In function ‘int main()’: stacktest.cpp:17

Choose template based on run-time string in C++

半腔热情 提交于 2019-11-27 15:00:43
问题 I have an attribute vector that can hold different types: class base_attribute_vector; // no template args template<typename T> class raw_attribute_vector : public base_attribute_vector; raw_attribute_vector<int> foo; raw_attribute_vector<std::string> foo; Based on run-time input for the type, I would like to create the appropriate data structure. Pseudocode: std::string type("int"); raw_attribute_vector<type> foo; Obviously, this fails. An easy, but ugly and unmaintainable workaround is a