Boost logo

Boost Users :

From: Joel de Guzman (joel_at_[hidden])
Date: 2007-01-10 19:05:00


Khalil Shalish wrote:
> Hello,
>
> I created the following composite class using mpl::inherit_linearly:
>
> template <class T>
> struct DataField {
> T data;
> }
>
> typedef mpl::inherit_linearly<
> mpl::vector< int, int, int>
> mpl::inherit< DataField<_2>, _1 >
>> Int3Composite;
>
> Int3Composite int3comp;
>
> Since the type of each data field is an int, I cannot use the type of the
> field to access the data in the field.
>
> Q: Is there a way to write a template (call it get_field) to access the data
> using numeric position in the type sequence. Something like this:
>
> int field_0 = get_field<0>(in3comp); //This returns the first int data field.
> int field_1 = get_field<1>(int3comp);
>
> I am very new to C++ template programming and any help on this will be very
> much appreciated.

Is the use of mpl::inherit_linearly merely to create a structure with
data from an MPL type sequence? If yes, you can simply use Fusion.
Example:

     typedef mpl::vector<int, int, int> types;
     fusion::result_of::as_vector<types>::type int3comp;

Then:

     fusion::get<0>(int3comp) = 1;
     fusion::get<1>(int3comp) = 2;
     fusion::get<2>(int3comp) = 3;

(Boost.Fusion has been recently accepted into boost and is now in the
Boost CVS)

Regards,

-- 
Joel de Guzman
http://www.boost-consulting.com
http://spirit.sf.net

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net