Note that there are some explanatory texts on larger screens.

plurals
  1. POC++0x Tuples Store Elements Backwards
    primarykey
    data
    text
    <p>After a bit of investigation, I found that C++0x stores the elements in a tuple backwards in memory.</p> <p>For example, take this code:</p> <pre>std::tuple&lt;char, char, char&gt; x('\0', 'b 'a'); char* y = (char*)&x; std::cout &lt;&lt; sizeof(x) &lt;&lt; std::endl; std::cout &lt;&lt; y &lt;&lt; std::endl;</pre> <p>When compiled with the GCC 4.5.2, I get the following output:</p> <pre>3 ab</pre> <p>This initially puzzled me. Why is the data stored backwards? After hunting through GNU's unintentionally obfuscated headers, I noticed that the implementation was similar to this:</p> <pre>template&lt;typename head, typename... tail&gt; class tuple&lt;head, tail...&gt; : public tuple&lt;tail...&gt; { head value; ... };</pre> <p>Because the base class contains the last element, then the next derived class contains the second to last, etc., the actual order of the template arguments is reversed.</p> <p>When I first got into tuples, I thought that I could use them for a function like <code>glInterleavedArrays()</code>, which sets an array of vertex data as tuples of colors, texture coordinates, normals, and points. Of course, if I make an array of tuples, this data will have to be inputted in reverse, which can result in really weird bugs if you happen to forget to put the arguments in the right order.</p> <p>What about something like this, then?</p> <pre>template&lt;typename... head, typename tail&gt; class tuple&lt;head..., tail&gt; : public tuple&lt;head...&gt; { tail value; ... };</pre> <p>Under the GCC 4.5.2:</p> <pre>error: parameter pack argument ‘head ...’ must be at the end of the template argument list</pre> <p>Unless this becomes available in the future, I'm pretty much stuck on finding another way to implement this. Is there another way? Some way to trick the GCC into getting a properly-ordered tuple memory-wise?</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.
 

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