[boost.mpl] Implementing a numeric index access to fields of a composite class created with mpl::inherit_linearly
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. Thank you, Khalil Shalish kshalish@austin.rr.com
Hi!
you could use an mpl::pair type to produce a static mapping of int value to type:
mpl::pair
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.
Thank you, Khalil Shalish kshalish@austin.rr.com _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
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
participants (3)
-
Joel de Guzman
-
Khalil Shalish
-
Ovanes Markarian