C++ Logo

std-proposals

Advanced search

Re: Conditional final class-virt-specifier

From: Arthur O'Dwyer <arthur.j.odwyer_at_[hidden]>
Date: Wed, 15 Jul 2020 16:31:55 -0400
On Wed, Jul 15, 2020 at 4:14 PM Paweł Benetkiewicz via Std-Proposals <
std-proposals_at_[hidden]> wrote:

> > No it doesn't. On none of the three major compilers does the code you've
> posted compile. Seen here: https://gcc.godbolt.org/z/EEeMae
> Wrong. It does, indeed. See here (MSVC 19.24):
> https://godbolt.org/z/9Tb313
>

Never take MSVC as your gold standard for anything involving templates.
They historically didn't even *parse* templates until instantiation time.
Since C++11, they've been forced to get a little better, but the
implementation is still basically "don't parse the thing until you see a
reason to do so." In this case, they're not parsing the `requires`-clause
until instantiation time.
MSVC still doesn't let you instantiate f<C>(), though. The syntax error is
correctly detected at instantiation time.
https://godbolt.org/z/Wvfa7W


> What you have to do is to enable C++20 support by using `/std:c++latest`
> compiler flag (which you did not).
> I don't know about other compilers besides MSVC and ECCP, which both work
> fine on all examples I've sent.
>

Since you have access to Godbolt, you can try your examples on Clang and
GCC as well.
Clang has the best support for C++20 concepts at the moment, *except* for
abbreviated function template syntax (`void f(auto)`).


>
> > Um, no it doesn't. Constraints of any kind do not require parsing the definition
> of the construct being constrained. Constraints are only based on the
> declaration. You cannot constrain a class template `T` on the properties
> of `T` itself.
> Wrong. See here: https://godbolt.org/z/Pjjrsd
> Compilation success, Compiled program runs and returns 0 as X exists.
>

Well, of course X exists! It's a template! But you have never instantiated
it.
If you try to instantiate X<bool>, MSVC crashes:
https://godbolt.org/z/W3qKhj
(Remember how I said never to take MSVC as evidence of anything where
templates are concerned?)

Every sane compiler rejects your code: https://godbolt.org/z/xqqso5
It happens for the same reason that the following happens:

// https://godbolt.org/z/ao9ac3
struct B {};
struct C : public C::Base {
    using Base = B;
};

In C++, *you cannot use a name before it has been declared.*
(Class members, used within the class body, are the one convenient
exception to this rule.)

HTH,
Arthur

Received on 2020-07-15 15:35:24