Question:
Where does the 'override' qualifier go with trailing return types?
David
2012-10-07 13:38:58 UTC
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?
Three answers:
anonymous
2012-10-11 04:25:16 UTC
struct Derived : public Base {

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

under
salguero
2016-12-26 20:02:55 UTC
nicely I went to the docs the different day, and informed him that i'm no longer feeling myself recently, the record reported, basically as nicely that's a nasty habit. No no I reported i've got self assurance like a pair of curtains, record reported Oh pull your self at the same time.
anekey
2016-12-11 21:17:03 UTC
properly I went to the docs the different day, and informed him that i'm not feeling myself recently, the checklist pronounced, just to boot that's a undesirable habit. No no I pronounced i believe like a pair of curtains, checklist pronounced Oh pull your self jointly.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...