Boost logo

Boost :

Subject: Re: [boost] Σχετ: [TypeSort] Automatic type sorting
From: Mathias Gaunard (mathias.gaunard_at_[hidden])
Date: 2015-03-20 12:32:04

On 20/03/2015 17:13, costis glynos wrote:

> On 20/03/2015 16:59, costis glynos wrote:
>>> Hello,Is there any interest in a library which automatically sorts types from smallest to largest at compile time?The benefit would be the automatic Data Structure Alignment and potential improvement in performance.Foo<int,bool,Custom16ByteType,char,double,short> foo_1; //normal
>>> Foo<int,bool,Custom16ByteType,char,double,short> foo_2; //using the TypeSort
>>> output:
>>> size of foo_1 = 48
>>> size of foo_2 = 32 Kind Regards,
>> There is already a sort meta-function in Boost.MPL.
>> <>
> Sweet! Thanks for bringing this to my attention.

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.

Boost list run by bdawes at, gregod at, cpdaniel at, john at