You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[Clang] Add a builtin that deduplicate types into a pack (#106730)
The new builtin `__builtin_dedup_pack` removes duplicates from list of
types.
The added builtin is special in that they produce an unexpanded pack
in the spirit of P3115R0 proposal.
Produced packs can be used directly in template argument lists and get
immediately expanded as soon as results of the computation are
available.
It allows to easily combine them, e.g.:
```cpp
template <class ...T>
struct Normalize {
// Note: sort is not included in this PR, it illustrates the idea.
using result = std::tuple<
__builtin_sort_pack<
__builtin_dedup_pack<int, double, T...>...
>...>;
}
;
```
Limitations:
- only supported in template arguments and bases,
- can only be used inside the templates, even if non-dependent,
- the builtins cannot be assigned to template template parameters.
The actual implementation proceeds as follows:
- When the compiler encounters a `__builtin_dedup_pack` or other
type-producing
builtin with dependent arguments, it creates a dependent
`TemplateSpecializationType`.
- During substitution, if the template arguments are non-dependent, we
will produce: a new type `SubstBuiltinTemplatePackType`, which stores
an argument pack that needs to be substituted. This type is similar to
the existing `SubstTemplateParmPack` in that it carries the argument
pack that needs to be expanded further. The relevant code is shared.
- On top of that, Clang also wraps the resulting type into
`TemplateSpecializationType`, but this time only as a sugar.
- To actually expand those packs, we collect the produced
`SubstBuiltinTemplatePackType` inside `CollectUnexpandedPacks`.
Because we know the size of the produces packs only after the initial
substitution, places that do the actual expansion will need to have a
second run over the substituted type to finalize the expansions (in
this patch we only support this for template arguments, see
`ExpandTemplateArgument`).
If the expansion are requested in the places we do not currently
support, we will produce an error.
More follow-up work will be needed to fully shape this:
- adding the builtin that sorts types,
- remove the restrictions for expansions,
- implementing P3115R0 (scheduled for C++29, see
cplusplus/papers#2300).
Copy file name to clipboardExpand all lines: clang/docs/ReleaseNotes.rst
+16Lines changed: 16 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -145,6 +145,22 @@ Non-comprehensive list of changes in this release
145
145
correct method to check for these features is to test for the ``__PTRAUTH__``
146
146
macro.
147
147
148
+
- Added a new builtin, ``__builtin_dedup_pack``, to remove duplicate types from a parameter pack.
149
+
This feature is particularly useful in template metaprogramming for normalizing type lists.
150
+
The builtin produces a new, unexpanded parameter pack that can be used in contexts like template
151
+
argument lists or base specifiers.
152
+
153
+
.. code-block:: c++
154
+
155
+
template <typename...> struct TypeList;
156
+
157
+
// The resulting type is TypeList<int, double, char>
158
+
using MyTypeList = TypeList<__builtin_dedup_pack<int, double, int, char, double>...>;
159
+
160
+
Currently, the use of ``__builtin_dedup_pack`` is limited to template arguments and base
161
+
specifiers, it also must be used within a template context.
162
+
163
+
148
164
New Compiler Flags
149
165
------------------
150
166
- New option ``-fno-sanitize-annotate-debug-info-traps`` added to disable emitting trap reasons into the debug info when compiling with trapping UBSan (e.g. ``-fsanitize-trap=undefined``).
0 commit comments