Skip to content

Commit c41bbfc

Browse files
committed
c++: only declare satisfied friends
A friend declaration can only have constraints if it is defined. If multiple instantiations of a class template define the same friend function signature, it's an error, but that shouldn't happen if it's constrained to only be declared in one instantiation. Currently we don't mangle requirements, so the foos all mangle the same and actually instantiating #1 will break, but for now we can test that they're considered distinct. gcc/cp/ChangeLog: * pt.cc (tsubst_friend_function): Check satisfaction. gcc/testsuite/ChangeLog: * g++.dg/cpp2a/concepts-friend11.C: New test.
1 parent e7c12a9 commit c41bbfc

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

gcc/cp/pt.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11284,6 +11284,9 @@ tsubst_friend_function (tree decl, tree args)
1128411284
not_tmpl = DECL_TEMPLATE_RESULT (new_friend);
1128511285
new_friend_result_template_info = DECL_TEMPLATE_INFO (not_tmpl);
1128611286
}
11287+
else if (!constraints_satisfied_p (new_friend))
11288+
/* Only define a constrained hidden friend when satisfied. */
11289+
return error_mark_node;
1128711290

1128811291
/* Inside pushdecl_namespace_level, we will push into the
1128911292
current namespace. However, the friend function should go
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// CWG2596
2+
// { dg-do compile { target c++20 } }
3+
4+
struct Base {};
5+
6+
int foo(Base&) { return 0; } // #0
7+
8+
template<int N>
9+
struct S : Base {
10+
friend int foo(Base&) requires (N == 1) { return 1; } // #1
11+
// friend int foo(Base&) requires (N == 2) { return 3; } // #2
12+
};
13+
14+
S<1> s1;
15+
S<2> s2; // OK, no conflict between #1 and #0
16+
int x = foo(s1); // { dg-error "ambiguous" }
17+
int y = foo(s2); // OK, selects #0
18+
19+
// ??? currently the foos all mangle the same, so comment out #2
20+
// and only test that #1 isn't multiply defined and overloads with #0.
21+
// The 2596 example does not include #0 and expects both calls to work.

0 commit comments

Comments
 (0)