fork download
  1. #include <iostream>
  2. #include <cassert>
  3. using namespace std;
  4.  
  5. struct Alfa { char t[1024]; };
  6. struct Base { char u[1024]; };
  7. struct Derived: Alfa, Base { char v[1024]; };
  8.  
  9. Derived* downcast(Base* b) {
  10. return static_cast<Derived*>(b);
  11. }
  12.  
  13. void test_bd(Base* b) {
  14. Derived* d = downcast(b);
  15. cout << "bd::" << b << " " << d << endl;
  16. }
  17.  
  18. void test_dbd(Derived* d) {
  19. Base* b = d;
  20. const char* isnull = (b == nullptr) ? "" : "!=nullptr";
  21. const char* iszero = (intptr_t)(b) == 0 ? "" : "!=zero";
  22. Derived* d1 = downcast(b);
  23. cout << "dbd:" << d << " " << b << isnull << iszero << " " << d1 << " " << endl;
  24. test_bd(b);
  25. }
  26.  
  27. void test_bdb(Base* b) {
  28. Derived* d = downcast(b);
  29. Base* b1 = d;
  30. cout << "bdb:" << b << " " << d << " " << b1 << endl;
  31. }
  32.  
  33. int main() {
  34. test_dbd((intptr_t)0);
  35. test_bdb((intptr_t)0);
  36. test_dbd((Derived*)(intptr_t)(-1024));
  37. test_bdb((Base*)(intptr_t)(1024));
  38. }
  39.  
Success #stdin #stdout 0s 4200KB
stdin
Standard input is empty
stdout
dbd:0 0 0 
bd::0 0
bdb:0 0 0
dbd:0xfffffffffffffc00 0!=nullptr!=zero 0xfffffffffffffc00 
bd::0 0
bdb:0x400 0 0