<html>
    <head>
      <base href="https://llvm.org/bugs/" />
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW --- - [Feature] Add a builtin for indexing into parameter packs"
   href="https://llvm.org/bugs/show_bug.cgi?id=25670">25670</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>[Feature] Add a builtin for indexing into parameter packs
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>All
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>All
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>C++
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedclangbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>ldionne.2@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>dgregor@apple.com, llvm-bugs@lists.llvm.org
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Created <span class=""><a href="attachment.cgi?id=15363" name="attach_15363" title="Initial version of the proposed patch.">attachment 15363</a> <a href="attachment.cgi?id=15363&action=edit" title="Initial version of the proposed patch.">[details]</a></span>
Initial version of the proposed patch.

This patch adds a `__nth_element` builtin that allows fetching the n-th type of
a parameter pack with very little compile-time overhead. The patch was inspired
by r252036 and r252115 by David Majnemer, which add a similar
`__make_integer_seq` builtin for efficiently creating a
`std::integer_sequence`. At its core, the builtin makes the following
specialization

__nth_element<i, T1, ..., Tn>

equivalent to Ti. Further analysis of the problem this patch is trying to solve
is available on a blog post I just wrote [1]. The post also present benchmarks
showing the compile-time improvement over existing techniques.

The patch is currently in a very rough state. It is my first time contributing
to Clang, and I encountered some issues that I hacked around just to get a
working solution. If there is interest in this patch, I will gladly improve it
until it is in an acceptable state, but I will most likely need some pointers
to do so. Things that are hacky right now:

1. I wasn't able to create a non-type template parameter from a builtin type.
Hence, `__nth_element` must currently be used as `__nth_element<TypeOfTheIndex,
Index, Ts...>` instead of just `__nth_element<Index, Ts...>`, while forcing
`Index` to be something like `unsigned long`. This way, I was able to copy
Majnemer's code that creates non-type template parameters. This is probably
trivial to change, but I couldn't figure it out immediately.

2. I wasn't sure how to retrieve an actual integer from an `APSInt`, so right
now I use `getExtValue()`. This is probably wrong, as I didn't really
understand what I was doing.

There are certainly other issues that a code review would address, if we make
it that far. Also, the name `__nth_element` is badly chosen, since it seems to
refer to the `nth_element` algorithm of the standard library. We should settle
on something else, but I have no preference.

Louis


[1]: <a href="http://ldionne.com/2015/11/29/efficient-parameter-pack-indexing">http://ldionne.com/2015/11/29/efficient-parameter-pack-indexing</a></pre>
        </div>
      </p>
      <hr>
      <span>You are receiving this mail because:</span>
      
      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>