Using 'override' for a function that has been declared using 'typedef'

Using 'override' for a function that has been declared using 'typedef'



C++ 11 introduced the 'override' specifier for functions and I find it useful as it makes it explicit that a virtual function is being overridden. However, I can't seem to get it work for a function that has been declared using a typedef.



I understand that 'override' is not a keyword, has it got something to do with that?



The following code illustrates my point:


#include <iostream>

typedef char ReturnsChar();

class Basic

public:
virtual char get_a();
virtual ReturnsChar get_z;
;

char Basic::get_a() return 'a';
char Basic::get_z() return 'z';

class Capitalized : public Basic

public:
// Can override explicitly if I use the normal definition
char get_a() override;

// Compiles if I use the typedef but not 'override'
ReturnsChar get_z;

// Will not compile, but would like to do this
//ReturnsChar get_z override;

;

char Capitalized::get_a() return 'A';
char Capitalized::get_z() return 'Z';

int main()

Basic foo;
Capitalized bar;

std::cout << foo.get_a() << std::endl; // a
std::cout << foo.get_z() << std::endl; // z
std::cout << bar.get_a() << std::endl; // A
std::cout << bar.get_z() << std::endl; // Z



I'm using GNU's g++ 8.2.0 and the error it gives me is


error: expected ';' at end of member declaration
ReturnsChar get_z override;
^~~~~
;
error: ‘override’ does not name a type; did you mean ‘ctermid’?
ReturnsChar get_z override;
^~~~~~~~
ctermid



EDIT: To address the comments, I understand this style is unclear. I am more interested about why this won't compile and what exactly 'override' does (especially because it is not a keyword).
As an aside, I feel like typedef-ing functions might be clear in some cases, say:


void (*foo(int x, void (*f)(int)))(int);



That's hard to read, especially if comes up often. I can just typedef that as 'UpdateAddressFunction' and then mentally think of every function of that type as something that 'updates an address'.





That is a really confusing / poor use of a typedef. That makes it very unclear within the class that get_z is a method and not a variable.
– CoryKramer
Aug 31 at 11:27


typedef


get_z





^ completely agree on that. If you add the language-lawyer tag someone might tell if this is according to the standard or not, but I dont want to see a code review that accepts this :P
– user463035818
Aug 31 at 11:31





Yes, in this example I agree completely. However, in my actual use case there are some very complex function prototypes and function pointers that I use, which make typedef-ing of a function useful (I like to think). Either way, the aim of my question is more about understanding why it will not compile, as opposed to style.
– Globe Theatre
Aug 31 at 11:31





why not typedef the returntype only? isnt that sufficient?
– user463035818
Aug 31 at 11:32






@user463035818 Another bug. A virt-specifier isn't even allowed in the syntax for a regular declaration, only in a member-declaration within a class.
– aschepler
Aug 31 at 11:45




1 Answer
1



I understand that 'override' is not a keyword, has it got something to do with that?



It does: override is only recognised in a few specific contexts and is available as a name for ordinary use otherwise. However, I believe this is a compiler bug, and this is one context where the compiler should recognise it in its special meaning.


override



The relevant grammatical production for this is



[class.mem]



member-declarator:

  declarator virt-specifier-seqopt pure-specifieropt



This does not require that a virt-specifier-seq only appear in a declaration containing parameters. The only similar requirement is stated below:



[class.mem]p8:



A virt-specifier-seq shall appear only in the declaration of a virtual member function (10.3).



In your case, the declaration is one of a virtual member function. I believe override should be accepted here.


override





The one catch is [lex.name]/2 "Unless otherwise specified, any ambiguity as to whether a given identifier has a special meaning is resolved to interpret the token as a regular identifier." I don't see a way that code could be ambiguous, but I'm not 100% certain.
– aschepler
Aug 31 at 11:39





Thanks, this is very clear. I'm quite pleased that I've discovered an actual compiler bug, that's one to tick off the bucket list!
– Globe Theatre
Aug 31 at 11:45





@aschepler Good point. I think there are some similar cases with ambiguities. I think struct A struct B ; struct B override; ; might be one, which because of what you quoted declares a non-static data member named override.
– user743382
Aug 31 at 11:45



struct A struct B ; struct B override; ;


override





Did you already file some bug reports?
– Rakete1111
Aug 31 at 15:41





@hvd Done: gcc.gnu.org/bugzilla/show_bug.cgi?id=87174
– Rakete1111
Aug 31 at 18:07



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Some of your past answers have not been well-received, and you're in danger of being blocked from answering.



Please pay close attention to the following guidance:



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

ャフサォクコ ケウ,コ,ワ メ,ロスョノ゙,クネ,フムカヤヲニ,エコ゚ツ ウイオン゙ケワサネォキモュキォウイノンコチ゚メヌナイゥフュ,カヒウネェ ネ,ホノケ,ムュキ ッボーミュハ,チ ツス ィ メウイマヤ,゙ウチ ヅ ロ,ォジヌェ ャヌット ェ,マャ,チナエヒネソキツテ トホヲヲミーァ

Node.js puppeteer - Use values from array in a loop to cycle through pages