Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter a tuple of types in c++17

std::tuple a{1,3,4,5} -> make it to numbers greater than 3

std::tuple b{4,5}    

Or

std::tuple a{
    std::integral_constant<int,1> {},
    std::integral_constant<int,3> {},
    std::integral_constant<int,4> {},
    std::integral_constant<int,5> {} 
}

to

std::tuple a{
    std::integral_constant<int,4>{},
    std::integral_constant<int,5>{}
};

How to convert this at compile time? I can do this using integer_sequence but that is a cumbersome. Is there a simpler way in C++17 using fold expressions or std::apply

Also after filter, also need to get a tuple of unique entries. But my assumption is if filtering can be done, then finding unique would be trivial.

Edit so that is more clear: std::tuple<int_c<1>, int_c<3>,int_c<4>,int_c<5>> to std::tuple<int_c<4>,int_c<5> <-- If such is possible in a concise c++17 way without extra declare functions, it would do!.

Edit: I was fiddling around, maybe something like this would work:

with template... C as the list of integrals constants:

constexpr auto result = std::tuple_cat(std::conditional_t<(C::value > 3), std::tuple<C>, std::tuple<>>{}...);
like image 582
themagicalyang Avatar asked Jun 21 '18 14:06

themagicalyang


1 Answers

To turn out your tuple_cat with c++17:

constexpr auto result = std::apply([](auto...ts) {
    return std::tuple_cat(std::conditional_t<(decltype(ts)::value > 3),
                          std::tuple<decltype(ts)>,
                          std::tuple<>>{}...);
}, tup);
like image 177
Jarod42 Avatar answered Oct 01 '22 09:10

Jarod42