Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. clangd warns on some methods without "override"

clangd warns on some methods without "override"

Scheduled Pinned Locked Moved Solved C++ Gurus
10 Posts 4 Posters 750 Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • JonBJ Offline
    JonBJ Offline
    JonB
    wrote last edited by JonB
    #1

    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 QAbstractTableModel and 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 & override against each one. Putting the virtuals at the start of each one is fine, and does not affect this issue. But as soon as I put override against 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 override suddenly cause other similar methods to require override too? I am OK with this behaviour --- in fact it's desirable for me --- but I did not expect it?

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote last edited by
      #2

      Maybe clangd ignores 'override' until it finds one in this class - either all or none should be marked but no inconsistence.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      JonBJ 1 Reply Last reply
      1
      • Christian EhrlicherC Christian Ehrlicher

        Maybe clangd ignores 'override' until it finds one in this class - either all or none should be marked but no inconsistence.

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote last edited by
        #3

        @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 marked override and others not. Just wanted to confirm my C++ understanding. Will mark yours as solution, though if anyone else knows better comments are still welcome.

        Christian EhrlicherC 1 Reply Last reply
        0
        • JonBJ JonB has marked this topic as solved
        • JonBJ JonB

          @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 marked override and others not. Just wanted to confirm my C++ understanding. Will mark yours as solution, though if anyone else knows better comments are still welcome.

          Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote last edited by
          #4

          @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.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          JonBJ 1 Reply Last reply
          1
          • Christian EhrlicherC Christian Ehrlicher

            @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.

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote last edited by
            #5

            @Christian-Ehrlicher
            Yes, thank you, I do know that. It was the clangd behaviour on some but not others which surprised me.

            1 Reply Last reply
            0
            • GrecKoG Offline
              GrecKoG Offline
              GrecKo
              Qt Champions 2018
              wrote last edited by
              #6

              Note that you shouldn't make overriden function as virtual because that's already implied by the override:
              C.128: Virtual functions should specify exactly one of virtual, override, or final

              JonBJ 1 Reply Last reply
              2
              • GrecKoG GrecKo

                Note that you shouldn't make overriden function as virtual because that's already implied by the override:
                C.128: Virtual functions should specify exactly one of virtual, override, or final

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote last edited by JonB
                #7

                @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 virtual against every virtual method, regardless of whether it's an override or not. And I like to see override against 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 for virtual and another for override. Plus I have to look at the start of the method definition line for virtual but at the end for override. :(

                unsigned also implies int, as override implies virtual, but if I feel like it I can choose to write unsigned int.

                Having said this: I note your reference states

                virtual means 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 virtual should not be there. Which would then leave me again to have to search the page for virtual separately from override if I want to know all of a class's virtual methods, whether they are new or overridden.

                1 Reply Last reply
                0
                • Chris KawaC Offline
                  Chris KawaC Offline
                  Chris Kawa
                  Lifetime Qt Champion
                  wrote last edited by Chris Kawa
                  #8

                  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 virtual is ambiguous (base or overridden). Using just override is 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.

                  JonBJ 1 Reply Last reply
                  3
                  • Chris KawaC Chris Kawa

                    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 virtual is ambiguous (base or overridden). Using just override is 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.

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote last edited by
                    #9

                    @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 KawaC 1 Reply Last reply
                    0
                    • JonBJ JonB

                      @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 KawaC Offline
                      Chris KawaC Offline
                      Chris Kawa
                      Lifetime Qt Champion
                      wrote last edited by
                      #10

                      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.

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved