Why does Qt use reinterpret_cast and not static_cast for void *?

You can use to / from any pointer to T to / from void * using static_cast, why does Qt use reinterpret_cast?

int SOME_OBJECT::qt_metacall(QMetaObject::Call _c, int _id, void **_a) { _id = QMainWindow::qt_metacall(_c, _id, _a); if (_id < 0) return _id; if (_c == QMetaObject::InvokeMetaMethod) { switch (_id) { // Why reinterpret_cast here?? case 0: on_tabWidget_tabCloseRequested((*reinterpret_cast< int(*)>(_a[1]))); break; default: ; } _id -= 1; } return _id; } 
+4
source share
2 answers

Honestly, I could never figure it out. The void ** structure is created in the same way, it simply drops the int* into void* , and then performs this weird cast on the other hand. As far as I can tell, static_cast will be not only beautiful, but also better.

You will find that in large projects such as Qt, there are a lot of dubious codes. Sometimes the material skips through the review or just keeps on, because no one wants to experience difficulties with its change.

+2
source

This is a bit outdated, but I disagree with the consensus. You cannot use static_cast to cast from any type to void* , you only do this with reinterpret_cast . static_cast reserved for types that are supposedly compatible, some compile-time checking is done (i.e. derived classes, numeric types between them). A.

With this MSDN link:

 dynamic_cast Used for conversion of polymorphic types. static_cast Used for conversion of nonpolymorphic types. const_cast Used to remove the const, volatile, and __unaligned attributes. reinterpret_cast Used for simple reinterpretation of bits. 
+1
source

Source: https://habr.com/ru/post/1340392/


All Articles