Use dynamic_cast for downcasting instead of static_cast. #12325

pull ghost wants to merge 1 commits into bitcoin:master from changing 9 files +10 −10
  1. ghost commented at 12:49 PM on February 1, 2018: none
  2. fanquake added the label Refactoring on Feb 1, 2018
  3. Use dynamic_cast for downcasting instead of static_cast.
    See: https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#c146-use-dynamic_cast-where-class-hierarchy-navigation-is-unavoidable
    fd355e29fe
  4. laanwj commented at 5:44 PM on February 1, 2018: member

    There's a sizable performance penalty in using dynamic_cast instead of static_cast due to the extra checks, and it needs RTTI type information enabled. I think it's unnecessary in all of these cases as the type of the object is already checked through qt's own introspection system. There's no need to do it through C++'s too.

  5. ghost commented at 5:53 PM on February 1, 2018: none

    Doubt there would be any performance issues in this particular case.

    Since Qt is a 3rd party library I wouldn't rely on static_casting it.

    If the code must compile with RTTI disabled I believe the Qt solution is qobject_cast https://doc.qt.io/qt-5/qobject.html#qobject_cast

  6. laanwj commented at 6:16 PM on February 1, 2018: member

    So do you see any concrete problems with the current code? Any realistic way this can fail the way it is now?

    If not, I don't see a reason to change it.

    We don't use dynamic_cast anywhere in the code and without a very good reason we shouldn't introduce it either.

  7. ghost commented at 6:30 PM on February 1, 2018: none

    Unless something happens inside Qt, the code will function correctly.

    I just have strong feeling about stuff like this.

  8. ajtowns commented at 6:20 AM on February 2, 2018: member

    The qt doc examples use static_cast -- http://doc.qt.io/qt-5/eventsandfilters.html so this seems fine as is, and even with these changes mistakes would still cause a crash since a nullptr returned by dynamic_cast will be immediately dereferenced. Mightn't be terrible to use dynamic_cast (or qobject_cast) in the if() statement instead of checking event->type(), but not sure it would be worth the effort.

  9. promag commented at 10:00 AM on February 6, 2018: member

    NACK. See ☝️ by @ajtowns.

  10. laanwj closed this on Feb 6, 2018

  11. laanwj commented at 12:39 PM on February 6, 2018: member

    Closing, there seems to be agreement to not do this

  12. DrahtBot locked this on Sep 8, 2021

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-17 15:15 UTC

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