8

I have a small program with such simple code:

namespace override
{
    using final = void();
}

namespace final
{
    using override = void(int);

    struct final
    {
        virtual override override;
        virtual ::override::final override;
    };
}

int main()
{
    struct final final : ::final::final
    {
        ::final::override override override final;
        ::override::final override final override;
    };

    // doesn't compile
    struct override : final
    {

    };        
}

Unfortunately it doesn't compile. I tried to compile it with following pieces of code:

// 1
struct override final
{

};

// 2    
override : final
{

};

And both of these variants compile fine. Is it an error in my compiler (clang 3.4)? I can't also understand why my original code doesn't compile.

See live example.

Update: It was an April Fool's day joke, of course. Thanks to all who participated in the discussion. I also thank @ecatmur for his exact but too serious answer.

I wanted to get the code that would have looked strange and at the same time would have compiled with one compiler at least (because it would have given respectability to my question). So my goal was not to create a standard-compliant code. But as @Johannes Schaub - litb noted in comments this code has at least one problem which make it ill-formed. The line virtual override override; is a violation the following rule (see paragraph [basic.scope.class] 3.3.7/1 of the standard):

A name N used in a class S shall refer to the same declaration in its context and when re-evaluated in the completed scope of S. No diagnostic is required for a violation of this rule.

As I can see it can be rewritten as virtual ::final::override override; to become a standard-compliant.

16
  • 2
    @JoachimPileborg An error is error: base 'final' is marked 'final'. What keywords do you mean? Commented Apr 1, 2014 at 17:20
  • 4
    @Jefffrey: override and final are no keywords. They are identifiers with special meaning when appearing in a certain context. Commented Apr 1, 2014 at 17:20
  • 2
    As far as I can tell you are marking final as final which means you can not use it as a base class ... do you have reason to believe this should not be the case? Commented Apr 1, 2014 at 17:31
  • 2
    @ShafikYaghmour But why do other variants compile? Commented Apr 1, 2014 at 17:32
  • 2
    Wow, all these serious answers. Am I really the only one around here who sees this as an April fools' prank? Commented Apr 1, 2014 at 17:53

1 Answer 1

14

Why would you expect it to compile? final is final, so you can't inherit from it.

In your other code:

struct override final
{

};

This is defining a final class with no base classes, so that's fine.

override : final
{

};

This is declaring a label override, and at that label creating a prvalue temporary of type final initialized with the brace-initializer {}, which is immediately destroyed:

override:
    final{};
Sign up to request clarification or add additional context in comments.

5 Comments

final is final? What do you mean here? Your explanation for other variants is very interesting. But label variant is so strange to me...
If a class is final, then it cannot be inherited from. This is a new feature in C++11.
Oh, in this meaning. I see. And what can you say about final final class contents?
It has two virtual functions both called override, one of type void() and the other of type void(int), both of which override the corresponding overload of override in the base class, and both of which are marked final so they can't be overridden in any further derived class.
Excellent! Happy April Fools' Day (or Night which is more likely)!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.