<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
</head>
<body bgcolor="#FFFFFF" text="#000000">
Hi all,<br>
<br>
(Not yet a list member, please CC me in all replies)<br>
<br>
I'm new to clang and am interested in adding a new compiler
intrinsic to simplify my work with variadic template parameter packs
in C++. The first half of this message explains the intrinsic I'm
hoping to add, and the second half is a plea for code pointers I
could use as a starting point. <br>
<br>
The motivation/description of the intrinsic can be illustrated by
the following C++17 code snippet:<br>
<br>
<font color="#333399"><tt>template <typename T></tt><tt><br>
</tt><tt>int hash(T const &t);</tt><tt><br>
</tt><tt>/* ^^^ to be overloaded for all relevant types */</tt><tt><br>
</tt><tt><br>
</tt><tt>template <template <typename...> class Tuple,
typename... T></tt><tt><br>
</tt><tt>int hash(Tuple<T...> const &t) </tt><tt><br>
</tt><tt>{</tt><tt><br>
</tt><tt> using std::get; // ADL</tt><tt><br>
</tt><tt> return ... ^ hash(get<<font color="#009900"><b>__indexof(T)</b></font>>(t));</tt><tt><br>
</tt><tt>}</tt></font><br>
<br>
The new intrinsic, __indexof(T) would only be valid inside an
expression that unpacks a parameter pack T. For each unpacked
expression that is produced, it would evaluate to a constexpr size_t
between 0 and sizeof...(T) giving the ordinal position of that
expression within the pack. Without such a functionality, you end up
writing a *lot* more (and a lot uglier) code to achieve the same
result when dealing with tuples. <br>
<br>
I'm floating this idea at the ISO std-proposals forum as a potential
addition for C++17---it's a great complement to fold
expressions---but I suspect it would get a lot more traction if I
could show a working implementation. Plus, then I could use it for
my own work in the meantime.<br>
<br>
Any pointers folks might have to parts of the code base I should be
studying as a starting point would be greatly appreciated. Currently
I'm digesting two things:<br>
<ul>
<li>Everything to do with the C++11 sizeof... operator. Hopefully
this will identify the bits of parsing and semantic analysis
that will need attention.</li>
<li>The changeset that introduced C++17 fold expressions.
Hopefully this will highlight locations that expand expressions
containing parameter packs. <br>
</li>
</ul>
<p>So far, it looks like the best approach will be to parse
__indexof(T) into a new IndexOfPackExpr object, and at an
appropriate moment later on---after the pack size is known but
before expansion begins---to replace that Expr with a non-type
template pack <size_t ...Indexes>, whose entries start at 0
and end just before sizeof...(T). That way, when expansion starts
the expression will contain two peer parameter packs of the same
size and the existing machinery will dutifully expand both in
lockstep.<br>
</p>
Does that sound like a reasonable approach?<br>
<br>
Thanks,<br>
Ryan Johnson<br>
<br>
</body>
</html>