clangd warns on some methods without "override"
-
Qt 6.4, Creator 13 and whatever clangd comes with it all as supplied with Ubuntu 24.04 release.
I create a new class derived from
QAbstractTableModeland select to insert its virtual methods. This produces:class MemoryModel : public QAbstractTableModel { // QAbstractItemModel interface public: int rowCount(const QModelIndex &parent) const; int columnCount(const QModelIndex &parent) const; QVariant data(const QModelIndex &index, int role) const; };I like all my stuff to be explicit so I want to insert
virtual&overrideagainst each one. Putting thevirtuals at the start of each one is fine, and does not affect this issue. But as soon as I putoverrideagainst the first one:virtual int rowCount(const QModelIndex &parent) const override;(It compiles fine but) clangd in the editor complains about each of the other lines that "it overrides a member function but is not marked 'override'". Why does marking one method
overridesuddenly cause other similar methods to requireoverridetoo? I am OK with this behaviour --- in fact it's desirable for me --- but I did not expect it? -
Maybe clangd ignores 'override' until it finds one in this class - either all or none should be marked but no inconsistence.
-
Maybe clangd ignores 'override' until it finds one in this class - either all or none should be marked but no inconsistence.
@Christian-Ehrlicher
OK, I agree, and as I said this actually suits me. I just wondered why it did given that it actually compiles fine with some markedoverrideand others not. Just wanted to confirm my C++ understanding. Will mark yours as solution, though if anyone else knows better comments are still welcome. -
J JonB has marked this topic as solved
-
@Christian-Ehrlicher
OK, I agree, and as I said this actually suits me. I just wondered why it did given that it actually compiles fine with some markedoverrideand others not. Just wanted to confirm my C++ understanding. Will mark yours as solution, though if anyone else knows better comments are still welcome.@JonB said in clangd warns on some methods without "override":
I just wondered why it did given that it actually compiles fine with some marked override and others not.
Because override is not mandatory and 'only' an additional hint for you and the compiler.
-
@JonB said in clangd warns on some methods without "override":
I just wondered why it did given that it actually compiles fine with some marked override and others not.
Because override is not mandatory and 'only' an additional hint for you and the compiler.
@Christian-Ehrlicher
Yes, thank you, I do know that. It was the clangd behaviour on some but not others which surprised me. -
Note that you shouldn't make overriden function as
virtualbecause that's already implied by theoverride:
C.128: Virtual functions should specify exactly one of virtual, override, or final -
Note that you shouldn't make overriden function as
virtualbecause that's already implied by theoverride:
C.128: Virtual functions should specify exactly one of virtual, override, or final@GrecKo
Thank you for pointing this out. Interesting but, to me, annoying. I note that document is "guidelines" rather than actual C++ requirement.I stick by my "styles" fairly closely. I like to see
virtualagainst every virtual method, regardless of whether it's an override or not. And I like to seeoverrideagainst every overridden (virtual) method. The recommended way breaks that for me, and makes it irritating if I wanted to, say, search for every virtual method in my code: I now have to do two searches, one forvirtualand another foroverride. Plus I have to look at the start of the method definition line forvirtualbut at the end foroverride. :(unsignedalso impliesint, asoverrideimpliesvirtual, but if I feel like it I can choose to writeunsigned int.Having said this: I note your reference states
virtualmeans exactly and only “this is a new virtual function.”Well, if that were true I would of course follow this rule. But if it is true how come compiler does not complain on
virtual ... override?I also note Qt's own documentation style:
int QLabel::heightForWidth(int w) const [override virtual]I know it's only an annotation, but by the guideline the
virtualshould not be there. Which would then leave me again to have to search the page forvirtualseparately fromoverrideif I want to know all of a class's virtual methods, whether they are new or overridden. -
The language doesn't require neither one of these keywords. It's a style choice if you use none, one or both and different tools will suggest differently. Using none is just bad for readability. Using just
virtualis ambiguous (base or overridden). Using justoverrideis terse and unambiguous so yes, using both is redundant. But to be fair I work in a codebase (Unreal Engine) that uses both as a style choice and, although redundant, I often find it easier to scan for visually.Core guidelines are not part of the standard. It's the preference of some big names in the C++ community, but that's it - their preference.
virtual means exactly and only “this is a new virtual function.”
That's not true at all, at least in the syntax sense. It means either that's a new virtual in the base class or an overridden in derived. The ambiguity of that is precisely why override was introduced, but it did not deprecate the usage of virtual on derived classes (it would break the world if it did).
As for clangd - Haven't looked into it, but I'd wager it comes down to heuristics - old code probably doesn't have either of those so clangd doesn't try to overspam you when it's plausible that this codebase just doesn't use that, but as soon as it sees at least one usage then it knows that first - your compiler is recent enough to support it (remember that it's a C++11 thing) and second - you either started to roll that into your codebase or just forgot to add it in another place, so it starts helping you out to locate inconsistencies.
My take is - pick style that the project you work in enforces. If it's you who decides then pick what suits you. If that happens to be in line with Core Guidelines - cool, if not - not a crime.
As for searching for overrides - I don't use text search for that if I can help it. Too unpredictable, as some libraries introduce their own macros over those keywords. A good code model that "understands" the code beyond the text is usually much more reliable for me.
-
The language doesn't require neither one of these keywords. It's a style choice if you use none, one or both and different tools will suggest differently. Using none is just bad for readability. Using just
virtualis ambiguous (base or overridden). Using justoverrideis terse and unambiguous so yes, using both is redundant. But to be fair I work in a codebase (Unreal Engine) that uses both as a style choice and, although redundant, I often find it easier to scan for visually.Core guidelines are not part of the standard. It's the preference of some big names in the C++ community, but that's it - their preference.
virtual means exactly and only “this is a new virtual function.”
That's not true at all, at least in the syntax sense. It means either that's a new virtual in the base class or an overridden in derived. The ambiguity of that is precisely why override was introduced, but it did not deprecate the usage of virtual on derived classes (it would break the world if it did).
As for clangd - Haven't looked into it, but I'd wager it comes down to heuristics - old code probably doesn't have either of those so clangd doesn't try to overspam you when it's plausible that this codebase just doesn't use that, but as soon as it sees at least one usage then it knows that first - your compiler is recent enough to support it (remember that it's a C++11 thing) and second - you either started to roll that into your codebase or just forgot to add it in another place, so it starts helping you out to locate inconsistencies.
My take is - pick style that the project you work in enforces. If it's you who decides then pick what suits you. If that happens to be in line with Core Guidelines - cool, if not - not a crime.
As for searching for overrides - I don't use text search for that if I can help it. Too unpredictable, as some libraries introduce their own macros over those keywords. A good code model that "understands" the code beyond the text is usually much more reliable for me.
@Chris-Kawa
Nice to see you :), your answer chimes with me.But to be fair I work in a codebase (Unreal Engine) that uses both as a style choice and, although redundant, I often find it easier to scan for visually.
Good to know I am not alone then.
virtual means exactly and only “this is a new virtual function.”
That's not true at all, at least in the syntax sense. It means either that's a new virtual in the base class or an overridden in derived.
Thank you.
As for searching for overrides - I don't use text search for that if I can help it. [...] A good code model that "understands" the code beyond the text is usually much more reliable for me.
Sometimes, like when you are marooned on a desert island, you don't always have a code model editor available....
-
@Chris-Kawa
Nice to see you :), your answer chimes with me.But to be fair I work in a codebase (Unreal Engine) that uses both as a style choice and, although redundant, I often find it easier to scan for visually.
Good to know I am not alone then.
virtual means exactly and only “this is a new virtual function.”
That's not true at all, at least in the syntax sense. It means either that's a new virtual in the base class or an overridden in derived.
Thank you.
As for searching for overrides - I don't use text search for that if I can help it. [...] A good code model that "understands" the code beyond the text is usually much more reliable for me.
Sometimes, like when you are marooned on a desert island, you don't always have a code model editor available....
Sometimes, like when you are marooned on a desert island
[in an announcer voice] Coming soon, to a desert island near you... :)
Yeah, I guess cases where all you have is text search on a big code base are not that uncommon, like when browsing github repo or something like that, but specifically overrides... I don't think I ever had to look for overrides this way. Oh well, different people different use cases.