|
Boost : |
Subject: [boost] ΣÏεÏ: [TypeSort] Automatic type sorting
From: costis glynos (cosglyn_at_[hidden])
Date: 2015-03-20 13:30:04
That's actually very useful! However a less complicated interface would definitely be more appreciated! Maybe some wrapper functions or macros would do the trick.
ΣÏÎ¹Ï 5:16 μ.μ. ΠαÏαÏκεÏ
ή, 20 ÎαÏÏίοÏ
2015, ο/η Mathias Gaunard <mathias.gaunard_at_[hidden]> ÎγÏαÏε:
On 20/03/2015 17:49, costis glynos wrote:
>>
>> You also need to use Boost.Fusion to make it into a tuple at the end,
>> since this is what you want.
>>
>> Here is an example:
>>
>> #include <boost/mpl/sort.hpp>
>> #include <boost/mpl/vector.hpp>
>> #include <boost/mpl/sizeof.hpp>
>> #include <boost/fusion/include/as_vector.hpp>
>> #include <boost/fusion/adapted/mpl.hpp>
>> #include <iostream>
>>
>> int main()
>> {
>>Â Â namespace mpl = boost::mpl;
>>Â Â namespace fusion = boost::fusion;
>>
>>Â Â typedef mpl::vector<short, int, short> types;
>>Â Â typedef mpl::sort<types, mpl::less< mpl::sizeof_<mpl::_1>
>>Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â , mpl::sizeof_<mpl::_2>
>>Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â >
>>Â Â Â Â Â Â Â Â Â Â Â >::type sorted_types;
>>Â Â typedef fusion::result_of::as_vector<sorted_types>::type tuple;
>>
>>Â Â tuple t;
>>Â Â std::cout << sizeof(t) << std::endl; // 8 instead of 12
>> }
>>
>> This appears to work.
> That's fantastic! Thank you for your recommendation.
You probably need to be able to access the final tuple with the original
indices though, which is a bit more involved.
#include <boost/mpl/sort.hpp>
#include <boost/mpl/vector.hpp>
#include <boost/mpl/range_c.hpp>
#include <boost/mpl/transform.hpp>
#include <boost/mpl/front.hpp>
#include <boost/mpl/back.hpp>
#include <boost/mpl/zip_view.hpp>
#include <boost/mpl/copy.hpp>
#include <boost/mpl/sizeof.hpp>
#include <boost/mpl/at.hpp>
#include <boost/fusion/include/as_vector.hpp>
#include <boost/fusion/include/at.hpp>
#include <boost/fusion/adapted/mpl.hpp>
#include <iostream>
int main()
{
 namespace mpl = boost::mpl;
 namespace fusion = boost::fusion;
 typedef mpl::vector<short, int, short> types;
 typedef mpl::copy<
  mpl::zip_view<
   mpl::vector< types
         , mpl::range_c<long, 0L, mpl::size<types>::value>
         >
  >
 , mpl::back_inserter< mpl::vector0<> >
 >::type indexes_types;
 typedef mpl::sort<
  indexes_types
 , mpl::less< mpl::sizeof_< mpl::front<mpl::_1> >
       , mpl::sizeof_< mpl::front<mpl::_2> >
       >
 >::type indexes_types_sorted;
 typedef mpl::transform<
  indexes_types_sorted
 , mpl::front<mpl::_1>
 >::type sorted_types;
 typedef mpl::transform<
  indexes_types_sorted
 , mpl::back<mpl::_1>
 >::type sorted_indexes;
 typedef fusion::result_of::as_vector<sorted_types>::type tuple;
 tuple t;
 std::cout << sizeof( fusion::at_c<mpl::at_c<sorted_indexes,
0>::type::value>(t) ) << std::endl;
 std::cout << sizeof( fusion::at_c<mpl::at_c<sorted_indexes,
1>::type::value>(t) ) << std::endl;
 std::cout << sizeof( fusion::at_c<mpl::at_c<sorted_indexes,
2>::type::value>(t) ) << std::endl;
}
This could make an interesting addition to Boost.Fusion.
_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk