Mathias Gaunard writes:
[...]
You probably need to be able to access the final tuple with the original
indices though, which is a bit more involved.
[...code...]
This could make an interesting addition to Boost.Fusion.
As a simple curiosity, here's how this could be implemented with Hana:
------------------------------------------------------------------------------
#include
#include
#include
#include
#include
#include <iostream>
namespace hana = boost::hana;
using namespace hana::literals;
int main() {
auto types = hana::tuple_t;
auto indexed_types = hana::zip(
types, hana::tohana::Tuple(hana::range(0_c, hana::size(types)))
);
auto sorted_indexed_types = hana::sort_by([](auto t, auto u) {
return hana::sizeof_(t[0_c]) < hana::sizeof_(u[0_c]);
}, indexed_types);
auto sorted_types = hana::transform(sorted_indexed_types, hana::head);
auto sorted_indices = hana::transform(sorted_indexed_types, hana::last);
using tuple = decltype(
hana::unpack(sorted_types, hana::template_hana::_tuple)
)::type;
tuple t;
std::cout << sizeof(t[sorted_indices[0_c]]) << std::endl;
std::cout << sizeof(t[sorted_indices[1_c]]) << std::endl;
std::cout << sizeof(t[sorted_indices[2_c]]) << std::endl;
}
------------------------------------------------------------------------------
It's essentially the same thing than with MPL/Fusion, except there's a bit
more syntactic sugar because of C++14 features. Also, this example showcases
how low the syntactic cost of the MPL/Fusion unification is. The only place
where we need to bridge between types and values is
using tuple = decltype(
hana::unpack(sorted_types, hana::template_hana::_tuple)
)::type;
which is (IMO) not too cumbersome.
Regards,
Louis
P.S.: I know the order of the arguments to sort_by is backwards. I'm working
on a consistent scheme for these algorithms.