Where does the ‘override’ qualifier go with trailing return types?

C++11 has the new override qualifier which can be applied to member functions to assert that they override a virtual function in the base class. C++11 also allows trailing return types so functions can be declared as auto f() -> return_type. When I combine both these features, I don’t know whether override goes before or after the ->.

For example, suppose we have the following base class:

struct Base {
    virtual auto f () const -> int = 0;
};

The two possibilities for a derived class are:

struct Derived : public Base {
    virtual auto f () const override -> int { return 0; } // Compiles on g++ 4.7.1
};

or

struct Derived : public Base {
    virtual auto f () const -> int override { return 0; } // Compiles on clang++ 4.0
};

g++ 4.7.1 compiles the first version but fails on the second with

test.cpp:6:30: error: expected ';' at end of member declaration
test.cpp:6:34: error: 'override' does not name a type

whereas clang++ 4.0 compiles the second one but fails on the first with

test.cpp:6:11: error: 'auto' return without trailing return type
  virtual auto f () const override -> int { return 0; }
          ^
test.cpp:6:3: error: only virtual member functions can be marked 'override'
  virtual auto f () const override -> int { return 0; }
  ^                       ~~~~~~~~
test.cpp:6:35: error: expected ';' at end of declaration list
  virtual auto f () const override -> int { return 0; }

Which of these compilers is actually doing the right thing according to the standard?

Edit: As Kerrek SB notes, this is a bug in gcc (Bugzilla link).

override with new function declaration syntax [duplicate]

This question already has an answer here: Where does the ‘override’ qualifier go with trailing return types? 1 answer Given the new function declaration syntax introduced in C++11, it is easy t

Does a placeholder in a trailing-return-type override an initial placeholder?

g++ appears to accept any combination of auto and decltype(auto) as initial and trailing return types: int a; auto f() { return (a); } // int auto g() -> auto { return (a); } // int auto h() ->

Trailing return types, decltype and const-ness

I was merily experimenting with the new trailing return types, where I hit a problem with this (simplified) code #include <list> class MyContainer{ std::list<int> ints; auto begin( ) ->

recursive trailing return types visual studio 2010

I’ve just upgraded to vc++ 2010 express from 2008 in order to make use of the c++11 features but I’m having problems using trailing return types in a recursive overloaded operator. I want to take two

Return from an anonymous immediately invoked function expression | Where does it go?

If I return an object from an anonymous immediately invoked function expression where does it go? For example where does foo go in this code…. (function(){ var foo; return foo; })(); Does this have

Using this and attributes in member function trailing return types?

In this answer I gave, it made sense to use this and the attribute of the class _arg in the trailing return type as part of the decltype expression. It’s possible to do without, but inconvenient. Neit

Where exactly does a python function’s return value go?

My Rock Paper Scissors code doesn’t work and I’m assuming it’s because I’m using the return value incorrectly. What should I do? EDIT, so I’ve stored the return like this def results (x, y): if (x ==

Why was the addition of trailing-return-types necessary in C++11?

I’ve finally started to read up on c++11 and I fail to understand why trailing-return-types are required. I came across the following example, which is used to highlight the problem: template<class

Where does java stderr go?

I would like to know, where does java stderr standardly go? I know I can change the stderr using System.setErr, which ‘Reassigns the standard error output stream.’, but I dont know, which one is st

Source Qualifier SQL Override Error with Mapping Variables

I am using a mapping variable of date/time data-type and aggregation MAX In Source Qualifier SQL Override i am giving select col1,col2 from tbl where col1>$$dt (col1 is also date data type) When i

Answers

According to the standard, 8.4.1, a declarator for a function includes the trailing-return-type, and a class function definition contains “declarator virt-specifier-seqopt“. The second one, virt-specifier-seq, is one of final or override, so those come after the trailing return type. (I.e. Clang gets it right.)