These are chat archives for boostorg/hana

24th
Aug 2016
Klemens Morgenstern
@klemens-morgenstern
Aug 24 2016 22:38
Hi, I've got a dumb question: is it not possible in general to use the hana-predicates with custom comparison operators? I've got it to work with a custom predicate, but the default ones give me this error (gcc 6): hana::less(x, y) requires 'x' and 'y' to be embeddable in a common Orderable. What am I doing wrong?
Jason Rice
@ricejasonf
Aug 24 2016 22:55
@klemens-morgenstern Do you have a minimal code snippet?
Klemens Morgenstern
@klemens-morgenstern
Aug 24 2016 23:04

Sure, definition of the comparison (including ==, !=, <=, >=, >):

template<typename T, typename U>
constexpr bool operator< (ctti<T>, ctti<U>) {return ::boost::typeindex::detail::constexpr_strcmp(ctti<T>::n(), ctti<U>::n()) < 0;}

That works:

struct ctti_greater_t
{
    template<typename T, typename U>
    constexpr std::integral_constant<bool,(ctti<T>() > ctti<U>())> operator()(ctti<T>,  ctti<U>) {return {};}
};

constexpr ctti_greater_t ctti_greater;

constexpr auto tup    = hana::make_tuple(ctti<int>(), ctti<double>(), ctti<void*>());
constexpr auto sorted = hana::sort(tup, ctti_greater_t);

That doesn't:

constexpr auto tup    = hana::make_tuple(ctti<int>(), ctti<double>(), ctti<void*>());
constexpr auto sorted = hana::sort(tup, hana::greater);

The full definition is here

This does allow sorting types by their name at compile-time.
Jason Rice
@ricejasonf
Aug 24 2016 23:12
Are you including boost/hana/ext/std/integral_constant.hpp? (or you could just return a hana::bool_)
Oh wait.. are you saying that using hana::greater is not working but your ctti_greater_t is?
Jason Rice
@ricejasonf
Aug 24 2016 23:22
This message was deleted
So really the answer is that you need to implement hana::less and optionally hana::greater I think.
Jason Rice
@ricejasonf
Aug 24 2016 23:29
ignore the detail::concepts thing I posted :P
Klemens Morgenstern
@klemens-morgenstern
Aug 24 2016 23:30
Ah ok. I hoped it would just use the plain operators as default. So I need to write a predicate anyway.
Thanks!
Jason Rice
@ricejasonf
Aug 24 2016 23:32
np
Jason Rice
@ricejasonf
Aug 24 2016 23:38
Perhaps consider having a high level function to convert ctti<T> to a hana::string. Then you could write:
constexpr auto sorted = hana::sort(tup, hana::greater ^hana::on^ my_to_string);
Klemens Morgenstern
@klemens-morgenstern
Aug 24 2016 23:55
I'm not sure that's possible without inserting hana stuff into boost/type_index (which I wouldn't like very much). Currently I get the string as a constexpr char* and from what I see I cannot construct a hana::string from that. But custom predictes will solve the issue. I was just wondering if I missed something.