[clang] [clang] Improve checking of operator functions (PR #131777)

via cfe-commits cfe-commits at lists.llvm.org
Tue Mar 18 07:53:55 PDT 2025


================
@@ -4120,6 +4121,28 @@ TemplateDeductionResult Sema::FinishTemplateArgumentDeduction(
     }
   }
 
+  // If the template is an operator function template, check that the
+  // resulting specialization is a valid operator function.
+  switch (Specialization->getOverloadedOperator()) {
+  case OO_None:
+  case OO_New:
+  case OO_Array_New:
+  case OO_Delete:
+  case OO_Array_Delete:
+    break;
+
+  default:
+    // SFINAE does not apply at this point in the instantiation process.
+    // Push a new CodeSynthesisContext to briefly re-enable it here.
+    InstantiatingTemplate Inst(
+        *this, Info.getLocation(), FunctionTemplate, DeducedArgs,
+        CodeSynthesisContext::DeducedTemplateArgumentSubstitution, Info);
+    if (Inst.isInvalid())
+      return TemplateDeductionResult::InstantiationDepth;
+    if (CheckOverloadedOperatorParams(Specialization))
+      return TemplateDeductionResult::SubstitutionFailure;
----------------
offsetof wrote:

No, this is just checking the synthesized declaration for validity. The intent is to capture diagnostics produced by `CheckOverloadedOperatorParams` and attach them to the "substitution failure" diagnostic related to `Info` (if one is emitted).

The standard wording basis for not making this a hard error is [[temp.over]/1.2](https://eel.is/c++draft/temp.over#1.2).


https://github.com/llvm/llvm-project/pull/131777


More information about the cfe-commits mailing list