Sam Kellett
Sam Kellett

Reputation: 1347

How do I filter a tuple of types depending on whether an expression is valid in Boost.Hana?

In a reduced example I have two types with in a tuple and I want to create another tuple that only contains the types where an expression is valid (in this example I'm using the + operator).

My attempt is like so:

#include <boost/hana.hpp>
#include <boost/hana/experimental/printable.hpp>

#include <iostream>

namespace hana = boost::hana;

struct foo {};

const auto result{hana::filter(hana::tuple_t<int, foo>, [](auto type) {
  const auto has_plus{hana::is_valid([](auto t)
    -> decltype((void) hana::traits::declval(t) + hana::traits::declval(t)) {})};

  return has_plus(type);
})};

int main()
{
  std::cout << hana::experimental::print(result) << std::endl;
}

This outputs '()', ie. no types matched where I would expect that the tuple result should contain type<int>.

I am using the Boost.Hana version that is packaged with Boost 1.62.

Upvotes: 2

Views: 202

Answers (1)

krzaq
krzaq

Reputation: 16431

Your problem is with operator precedence.

(void)hana::traits::declval(t) + hana::traits::declval(t)

is the same as

((void)hana::traits::declval(t)) + hana::traits::declval(t)

The following yields the result you expected:

(void)(hana::traits::declval(t) + hana::traits::declval(t))

demo

Upvotes: 4

Related Questions