Use the override specifier (C++11) where we expect to be overriding the virtual function of a base class #10631

pull practicalswift wants to merge 1 commits into bitcoin:master from practicalswift:overrides-ii changing 18 files +68 −68
  1. practicalswift commented at 3:07 PM on June 19, 2017: contributor

    Use the override specifier (C++11) where we expect to be overriding the virtual function of a base class.

    Rationale: By using the override specifier (C++11) we're guaranteed a compile-time error if our stated override expectation is not fulfilled. This avoids gotchas with regards to function renames, function signature changes, etc. Additionally it clarifies the intended behaviour for the reader.

  2. practicalswift force-pushed on Jun 19, 2017
  3. practicalswift force-pushed on Jun 19, 2017
  4. fanquake added the label Refactoring on Jun 20, 2017
  5. TheBlueMatt commented at 4:37 PM on June 20, 2017: member

    Nice! Is there an automated way I can check that you didn't miss any, or did you do it by hand?

  6. theuni commented at 6:16 PM on June 20, 2017: member

    -Wsuggest-override should do it, though google says it may be inconsistent wrt 'final'.

  7. theuni commented at 6:50 PM on June 20, 2017: member

    https://github.com/theuni/bitcoin/commit/9b1f7626a4c16924d0921eb822efc40f77212f8c adds those suggested by gcc. Note that I just blindly added them, didn't verify correctness.

    There are a bunch more in qt, even ignoring protobufs.

    Concept ACK, but without being able to enforce, I fear that this might turn into another -Wshadow that drove @laanwj crazy.

  8. practicalswift commented at 7:09 PM on June 20, 2017: contributor

    @theuni Can you give some context for the -Wshadow incident?

  9. MarcoFalke commented at 7:23 PM on June 20, 2017: member

    @practicalswift Different compilers and different versions of the same compiler have non-matching notions of what "shadow" means. Some of them even had false positives. Effectively it was impossible to enforce this rule when the code was written by a developer and we had weekly cleanup pulls to "fix wshadow warnings". Let's avoid that this turns into a avalanche of "add override specifier" pulls.

  10. practicalswift commented at 7:25 PM on June 20, 2017: contributor

    @MarcoFalke Agreed! Just out of curiosity, has anyone tried to add proper override specifiers (project wide) before?

  11. practicalswift force-pushed on Jun 20, 2017
  12. practicalswift force-pushed on Jun 20, 2017
  13. practicalswift commented at 8:30 PM on June 20, 2017: contributor

    @TheBlueMatt I believe the current version is 100 % complete with the exception of the src/qt/ and src/univalue/ directories which I omitted intentionally. Please let me know if you find any counterexamples – cases which are not marked override in this PR but for which override should be used.

  14. TheBlueMatt commented at 12:57 AM on June 21, 2017: member

    utACK d39888d178fd72235c2aeedb97d3b7f99260da38

  15. sipa commented at 1:03 AM on June 21, 2017: member

    As adding override can never cause a behaviour change, utACK-because-it-compiles d39888d178fd72235c2aeedb97d3b7f99260da38

  16. laanwj commented at 4:38 PM on June 21, 2017: member

    @theuni Can you give some context for the -Wshadow incident?

    Discussion started here: #8105 (comment) As for the fallout: https://github.com/bitcoin/bitcoin/issues?utf8=%E2%9C%93&q=is%3Aissue%20is%3Aclosed%20wshadow https://github.com/bitcoin/bitcoin/pulls?q=is%3Apr+wshadow+is%3Aclosed

    It got increasingly heated over time, until we finally decided to hack the warning out again.

    Concept ACK, but without being able to enforce, I fear that this might turn into another -Wshadow that drove @laanwj crazy.

    Do compilers agree on when override should be used? We have a few uses of override in the code since #10550 and I don't think it has given any issues. I thnk I've only seen warnings from when override was applied to part of the methods instead of the entire class. Clang does this. I don't think this can be as bad as Wshadow, where different versions of compilers had different interpretations. Also new classes aren't introduced that frequently. So I think it's ok...

  17. theuni commented at 7:02 PM on June 21, 2017: member

    @laanwj I'm not worried about adding overrides, I definitely think we should!

    I was only suggesting that because we can't enforce them with static analysis, we'll occasionally forget them and end up with one-off PRs like this one. Which is fine by me :)

    clang/gcc both have warnings that can be enabled, but sadly we can't use them by default because:

    • Apparently gcc still warns about missing override when 'final' is used instead
    • Our dependencies don't necessarily use them, so we're flooded with warnings for qt/protobufs/leveldb.
  18. laanwj commented at 2:15 PM on June 24, 2017: member

    Apparently gcc still warns about missing override when 'final' is used instead

    From what I understand, override marks a method as being overridden. final marks it as non-overridable. As they work in the opposite direction through the inheritance hierarchy I don't see why one would be a replacement for the other?

    Our dependencies don't necessarily use them, so we're flooded with warnings for qt/protobufs/leveldb.

    That's a good point. Let's indeed not enable those warnings by default, just try to use it in our own code where we can (as this PR does).

  19. gmaxwell commented at 8:26 PM on June 27, 2017: contributor

    @sipa I thought the value of adding override is catching some bugs that would arise through renames. As a result blinding adding it wouldn't be a great idea, even though it can never cause a change in behavior because doing so would undermine the advantage of it, no?

  20. theuni commented at 9:08 PM on June 27, 2017: member

    @gmaxwell I believe @sipa meant the fact that it compiles is good enough because 'override' triggers an error if it doesn't actually override anything. If the function signature ever changes (on accident), it will no longer override its base, and will fail to compile.

    I looked these up to make sure that they're actual errors rather than being left up to compilers to decide:

    §10.3.5

    If a virtual function is marked with the virt-specifier override and does not override a member function of a base class, the program is ill-formed.

    §9.2.9:

    A virt-specifier-seq shall contain at most one of each virt-specifier. A virt-specifier-seq shall appear only in the declaration of a virtual member function.

  21. sipa commented at 11:00 PM on June 27, 2017: member

    Needs rebase.

  22. Use the override specifier (C++11) where we expect to be overriding the virtual function of a base class aa95947ded
  23. practicalswift force-pushed on Jun 28, 2017
  24. practicalswift commented at 12:12 AM on June 28, 2017: contributor

    Rebased! :-)

  25. laanwj assigned laanwj on Jun 28, 2017
  26. laanwj merged this on Jun 28, 2017
  27. laanwj closed this on Jun 28, 2017

  28. laanwj referenced this in commit 9a941a1010 on Jun 28, 2017
  29. PastaPastaPasta referenced this in commit 357cb3af95 on Jul 6, 2019
  30. PastaPastaPasta referenced this in commit 61cbfef59e on Jul 6, 2019
  31. PastaPastaPasta referenced this in commit faf8c63221 on Jul 6, 2019
  32. PastaPastaPasta referenced this in commit 803c3dcacf on Jul 8, 2019
  33. PastaPastaPasta referenced this in commit 28abe148d9 on Jul 9, 2019
  34. PastaPastaPasta referenced this in commit e7f8599bd1 on Jul 11, 2019
  35. PastaPastaPasta referenced this in commit 4926dfab97 on Jul 13, 2019
  36. PastaPastaPasta referenced this in commit 9bcadebf77 on Jul 17, 2019
  37. barrystyle referenced this in commit 1a11fe8843 on Jan 22, 2020
  38. practicalswift deleted the branch on Apr 10, 2021
  39. DrahtBot locked this on Aug 16, 2022

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-04-16 15:15 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me