[PATCH] D43357: [Concepts] Function trailing requires clauses

Richard Smith - zygoloid via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Aug 7 14:08:43 PDT 2018


rsmith added inline comments.


================
Comment at: lib/Sema/SemaDecl.cpp:8377-8381
+      } else if (D.hasTrailingRequiresClause()) {
+        // C++2a [class.virtual]p6
+        // A virtual method shall not have a requires-clause.
+        Diag(NewFD->getTrailingRequiresClause()->getLocStart(),
+             diag::err_constrained_virtual_method);
----------------
saar.raz wrote:
> rsmith wrote:
> > This is the wrong place for this check. We don't yet know whether the function is virtual here in general. A function can become virtual due to template instantiation:
> > 
> > ```
> > template<typename T> struct A : T { void f() requires true; };
> > struct B { virtual void f(); };
> > template struct A<B>; // error, A<B>::f is constrained and virtual
> > ```
> > 
> > This is perhaps a wording defect: it's not clear that `A::f()` really should override `B::f()`, but that is the consequence of the current rules. I've posted a question to the core reflector.
> I don't really see why A::f() should override B::f() indeed - since it is not marked virtual nor override, shouldn't it just hide B::f()? or am I missing something here?
Functions override base class virtual functions if they have matching name, parameter types, cv-qualifiers and ref-qualifiers, *regardless* of whether they're declared `virtual` etc. Eg:

```
struct A { virtual void f(); };
struct B : A { void f(); };
```

`B::f` overrides `A::f`, and as a consequence, `B::f` is implicitly a virtual function. See [class.virtual]p2 and its footnote.

Note that the determination of whether the derived class function overrides the base class function doesn't care about the associated constraints on the derived class function. (After checking with the core reflector, general consensus seems to be that this is the right rule, and my previous example should indeed be ill-formed because it declares a constrained virtual function.)


Repository:
  rC Clang

https://reviews.llvm.org/D43357





More information about the cfe-commits mailing list