What MakeTypelist<...>::Result ge...

Developers
void
2010-06-09
2013-04-08
  • void
    void
    2010-06-09

    Hello,
    I have a question about what the template MakeTypelist expected to generate exactly. I have
    specific example:

    typedef Tuple< MakeTypelist<uint16_t, uint16_t, uint16_t, uint8_t, uint8_t,  uint8_t, uint8_t, uint8_t, uint8_t, uint32_t>::Result >
    my_data;

    I expect the code output for this type be equivalent (in terms of layout and sizes, not member names etc)  to :

    typedef struct
    {
      struct { uint16_t _a; } a;
      struct { uint16_t _b; } b;
      struct { uint16_t _c; } c;
      struct { uint8_t _d; } d;
      struct { uint8_t _e; } e;
      struct { uint8_t _f; } f;
      struct { uint8_t _g; } g;
      struct { uint8_t _h; } h;
      struct { uint8_t _j; } j;
      struct { uint32_t _k; } k;
    }s_my_data;

    I expect essentially the order,  but also final size,  of my_data and s_my_data to be exactly same.
    And this is not the case on my compiler.

    Do I expect wrong things from this template?  Any help here?

     
  • void
    void
    2010-06-09

    I can take Tuple out of the equation  rather:

    typedef  MakeTypelist<uint16_t, uint16_t, uint16_t, uint8_t, uint8_t, uint8_t, uint8_t, uint8_t, uint8_t, uint32_t>::Result >  my_data;

    BOOST_STATIC_ASSERT( sizeof(my_data)  == sizeof(s_my_data)); //FAILS

     
  • Fraser
    Fraser
    2010-06-09

    Typelists hold types not data members.  You have a misunderstanding.  If you need information on Typelists it can be found in Modern C++ Design.

     
  • void
    void
    2010-06-09

    How about this one:

    my_data d;
    s_my_data d2;

    BOOST_STATIC_ASSERT( sizeof(d) == sizeof (d2));

    d, and d2 are concrete variables now

     
  • void
    void
    2010-06-09

    Ooch, I see that my use of Typelists is odd.

    Could you please refer to my post 1, where I wrap this inside Tuple ? :)

     
  • Hi v01d75

    When you wrote in your first post that "I expect the code output for this type be equivalent (in terms of layout and sizes, not member names etc) to" a struct, I think you are assuming that typelists are equivalent to structs.  They are not equivalent at all.

    Typelists only exist at compile time, while structs are used to store runtime data.

    A typelist is conceptually similar to a singly linked lists of datatypes and so the "size of" a typelist depends on the size of the first entry in the list.  On the other hand, all the data members of a struct exist at the same level (not in a list) so the size of the struct depends on the size of all the objects plus any padding to compensate for byte or word alignment.

    Typelists have many uses, but using them like a struct is not one them.  (Wrong tool for that job.)

    I hope this answers your questions.  If not, feel free to rephrase your questions.  Another option if you want to learn more about typelists, you can read the "Modern C++ Design" book as fraserross suggested.  You can also read about them on various internet forums.

    Cheers,

    Rich

     
  • void
    void
    2010-06-10

    Right,

    Many thanks for your replies.

    I see its a wrong tool for job. I did hope however to abuse :) the feature, and go from compile-time to run-time object containing same types as compile time one.

    The alighment, padding, optimization of course affect sizes, but being aware of those I still wanted to use Tuple & Typelist
    as it looked neat.
    I also looked at wrong place, Tuple, and the unit, which are all structs.

    Thanks !