Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to accomplish this complex mapping with template metaprogramming?
    primarykey
    data
    text
    <p>I'm trying to define a struct using template metaprogramming but what I'm trying to do may need to be spread over multiple structs instead of having one super struct:</p> <pre><code>template &lt;A, B, C&gt; struct ABC { A a; B b; C c; } </code></pre> <p>Here's the flexible I hope to achieve as I will be using a code-generator and would prefer not to have to define each possible struct-type concretely if at all possible:</p> <ol> <li>Types for A, B and C can be defined.</li> <li>a, b or c can be pre-defined and pre-assigned constants (i.e, const unsigned char a = 0x48; ) or be regular variable member types.</li> </ol> <p>The general pattern is I will have unsigned numeric input for b in my code-generator, c will generally be integral (of 1-8 bytes in length) and a will be the resulting total length of the struct.</p> <p>Some examples (not exactly what is needed but to give an idea):</p> <pre><code>struct &lt;struct1&gt; { const unsigned char a = 'A'; const unsigned short b = 0x1000; char c[10]; } </code></pre> <p>I don't know enough about template meta-programming to even begin going about doing this particularly for the a and b parts where they can be pre-assigned or not.</p> <p>Perhaps to keep it easy, we can get away with assuming that I will always assign values to A, B, C and a, and b so a and b can be constants and pre-assigned.</p> <p>In the end these messages will be casted to char* and sent over the wire (and byte-packed using #pragma)</p> <p>Thanks in advance for your help!</p> <p>Sorry for the confusing problem description. I will try to clarify it with some examples of what I would like to accomplish using the templated struct ABC (or with additional structs defined as needed). These examples are from the end-user perspective:</p> <pre><code>typedef ABC&lt;100, char[10]&gt; Msg1; </code></pre> <p>would result in the equivalent of:</p> <pre><code>struct Msg1 { const unsigned short a = sizeof(Msg1); // sizeof(Msg1) fits in unsigned short const unsigned char b = 100; char[512] c; } </code></pre> <p>Note the size of members a and b need to be determined by the template based on the size of Msg1 and the passed in parameter 100. This is the tricky part for me.</p> <pre><code>typedef ABC&lt;23000, unsigned int&gt; Msg2; struct Msg2 { const unsigned char a = sizeof(Msg2); // sizeof(Msg2) fits in unsigned char const unsigned short b = 23000; unsigned int c; } </code></pre> <p>Hope this makes it a bit more clear.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload