Similar to #10498 , abide by the CppCodeGuidelines to enforce type safe casts.
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-
ghost commented at 12:49 PM on February 1, 2018: none
- fanquake added the label Refactoring on Feb 1, 2018
-
fd355e29fe
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
-
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.
-
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
-
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.
-
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.
-
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. - laanwj closed this on Feb 6, 2018
-
laanwj commented at 12:39 PM on February 6, 2018: member
Closing, there seems to be agreement to not do this
- DrahtBot locked this on Sep 8, 2021