title: Structured binding on an unknown custom structure
body: <p><strong>Short version:</strong></p>
  
  <p>I would like to be able to convert a struct to a tuple. At least the type. In the code below, the <em>convertToTuple</em> function does not work, because variadic parameters cannot be used in structured bindings (as far as I know). The key line is: <em>auto&amp; [values...] = value;</em></p>
  
  <pre><code>struct Vec3 {
   float x;
   float y;
   float z;
  };
  template &lt;typename T&gt;
  auto structToTuple(T &amp;value) {
   auto&amp; [values...] = value;  //doesn't work
   return std::make_tuple&lt;decltype(values)...&gt;(values...);
  }
  Vec3 v;
  std::tuple&lt;float, float, float&gt; t = structToTuple(v);
  </code></pre>
  
  <p>Basically, what I need is a means to convert the type of a custom structure to a tuple, which contains all of the types from the struct. For example:</p>
  
  <pre><code>struct Ray {Vec3, Vec3} -&gt; std::tuple&lt;
   std::tuple&lt;float, float, float&gt;,
   std::tuple&lt;float, float, float&gt;&gt;;
  </code></pre>
  
  <p><strong>Detailed problem:</strong></p>
  
  <p>I would like to create a templated function which takes a type or a list of types as template parameter and produces a list of textures, each texture containing an item. An other function can sample the texture list and pack the values back together to return the same type. For example, if I have a type:</p>
  
  <pre><code>std::pair&lt;std::tuple&lt;int, int&gt;, int&gt; value;
  std::tuple&lt;Texture&lt;int, int&gt;, Texture&lt;int&gt;&gt; tex = createTexture(value);
  std::pair&lt;std::tuple&lt;int, int&gt;, int&gt; thisshouldwork = sample(tex);
  </code></pre>
  
  <p>The above code is just a simple example of what I want to do, not my actual code. In this case 2 textures would be created, one texture would contain the two ints from the tuple, the other would hold a single int type. My intention is to hide the texture handling behind an interface, where I can store an arbitrary value (as long as it's composed of some simple types) in textures that I can upload to the GPU for the shaders.
  As long as I only use std::tuple, and std::pair, it does work, because I can extract the types from them:</p>
  
  <pre><code>template &lt;typename... Args&gt;
  void f(std::tuple&lt;Args...&gt; t);
  </code></pre>
  
  <p>I would like to be able to do the same when the template parameter is a custom struct. For example:</p>
  
  <pre><code>struct Vec3 {
   float x;
   float y;
   float z;
  };
  Vec3 v;
  Texture&lt;float, float, float&gt; tex = createTexture(v);
  
  struct Ray{
   Vec3 pos;
   Vec3 dir;
  };
  Ray r;
  std::tuple&lt;Texture&lt;float,float,float&gt;, Texture&lt;float,float,float&gt;&gt; tex2 = createTexture(r);
  </code></pre>
  
  <p>I am not convinced that this is even possibly with the current C++ standard, but based on the structured bindings, it seems possible.
  My idea would be something like this:</p>
  
  <pre><code>template &lt;typename T&gt;
  auto structToTuple(T &amp;value) {
   auto&amp; [values...] = value;
   return std::make_tuple&lt;decltype(values)...&gt;(values...);
  }
  Vec3 v;
  std::tuple&lt;float, float, float&gt; t = structToTuple(v);
  </code></pre>
  
  <p>As far as I am concerned variadic parameters only work function or template parameters. But if the <em>structToTuple</em> function would work, that would solve my problem.</p>
  
  <p>Thanks for the help in advance, guys!</p>
  
  <p><strong>Update:</strong></p>
  
  <p>I have found a workaround for my problem (not a general solution):
  <a href="https://github.com/Dwarfobserver/AggregatesToTuples/blob/master/single_include/aggregates_to_tuples.hpp" rel="nofollow noreferrer">https://github.com/Dwarfobserver/AggregatesToTuples/blob/master/single_include/aggregates_to_tuples.hpp</a></p>
  
  <p>The author of this library defined a structure to tuple conversion, but it only works if the structure has no more than 50 parameters. This solves my problem in practice, though I am still curious whether it is possible with an arbitrary structure.</p>