[clang] [clang] Find conversion function templates for in-class specializations (PR #218316)
via cfe-commits
cfe-commits at lists.llvm.org
Sun Aug 23 21:36:08 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Kunal Dubey (xakep8)
<details>
<summary>Changes</summary>
Allow redeclaration lookup to consider conversion function templates allowing Clang to match an in-class specialization such as `template<> operator int()` against a prior conversion function template `template<class T> operator T()`.
Fixes #<!-- -->218261
---
Full diff: https://github.com/llvm/llvm-project/pull/218316.diff
2 Files Affected:
- (modified) clang/lib/Sema/SemaLookup.cpp (+1-1)
- (modified) clang/test/SemaCXX/conversion-function.cpp (+18)
``````````diff
diff --git a/clang/lib/Sema/SemaLookup.cpp b/clang/lib/Sema/SemaLookup.cpp
index 43129800e9813..b385392150d6c 100644
--- a/clang/lib/Sema/SemaLookup.cpp
+++ b/clang/lib/Sema/SemaLookup.cpp
@@ -1158,7 +1158,7 @@ static bool LookupDirect(Sema &S, LookupResult &R, const DeclContext *DC) {
// name lookup. Instead, any conversion function templates visible in the
// context of the use are considered. [...]
const CXXRecordDecl *Record = cast<CXXRecordDecl>(DC);
- if (!Record->isCompleteDefinition())
+ if (!Record->isCompleteDefinition() && !R.isForRedeclaration())
return Found;
// For conversion operators, 'operator auto' should only match
diff --git a/clang/test/SemaCXX/conversion-function.cpp b/clang/test/SemaCXX/conversion-function.cpp
index 717c73c4786eb..e00553fbb20a3 100644
--- a/clang/test/SemaCXX/conversion-function.cpp
+++ b/clang/test/SemaCXX/conversion-function.cpp
@@ -473,6 +473,24 @@ struct S {
};
}
+#if __cplusplus >= 201103L
+namespace GH218261 {
+ struct S {
+ template <typename T>
+ constexpr operator T() const {
+ return 10;
+ }
+
+ template <>
+ constexpr operator int() const {
+ return 4;
+ }
+ };
+
+ static_assert(S().operator int() == 4, "");
+}
+#endif
+
#if __cplusplus >= 201103L
namespace dependent_conversion_function_id_lookup {
namespace gh77583 {
``````````
</details>
https://github.com/llvm/llvm-project/pull/218316
More information about the cfe-commits
mailing list