[clang] [Clang] Consider default template arguments when synthesizing CTAD guides (PR #147675)
Younan Zhang via cfe-commits
cfe-commits at lists.llvm.org
Fri Jul 11 01:29:52 PDT 2025
================
@@ -1061,15 +1061,36 @@ BuildDeductionGuideForTypeAlias(Sema &SemaRef,
SmallVector<DeducedTemplateArgument> DeduceResults(
F->getTemplateParameters()->size());
+ // We don't have to deduce against the alias template specialization,
+ // if the source template is a synthesized alias deduction guide. This allows
+ // us to utilize the default template arguments from alias declaration.
+ //
+ // template <class T>
+ // using Foo = A<A<T>>;
+ //
+ // template <class U = int>
+ // using Bar = Foo<U>;
+ //
+ // In terms of Bar, we want U to appear in the synthesized deduction guide,
+ // but U would remain undeduced if we deduce against A<T> instead of T.
+ // Also note that since the deduced results are only used for synthesizing
+ // template parameters, they should not introduce unintended behavior in
+ // theory.
+ ArrayRef<TemplateArgument> Ps = FReturnType->template_arguments();
+ if (auto *DG = dyn_cast<CXXDeductionGuideDecl>(F->getTemplatedDecl());
+ DG && DG->getSourceDeductionGuideKind() ==
+ CXXDeductionGuideDecl::SourceDeductionGuideKind::Alias)
+ Ps = F->getInjectedTemplateArgs(Context);
----------------
zyn0217 wrote:
I finally figured out another way to address it: we don't need to bother looking at the injected template arguments.
https://github.com/llvm/llvm-project/pull/147675
More information about the cfe-commits
mailing list