fork(17) download
  1. #include <iostream>
  2. #include <typeinfo>
  3.  
  4. struct A { virtual void Foo() {} };
  5. struct B : public A { void Foo() {} };
  6. struct C : public A { };
  7.  
  8. int main() {
  9. std::cout << "naive - fails" << std::endl;
  10. std::cout << int(&A::Foo == &A::Foo) << std::endl;
  11. std::cout << int(&A::Foo == &B::Foo) << std::endl;
  12. std::cout << int(&A::Foo == &C::Foo) << std::endl;
  13.  
  14. std::cout << "needs gcc-only PMF extension - semi-works" << std::endl;
  15. std::cout << int(reinterpret_cast<void(*)()>(&A::Foo) == reinterpret_cast<void(*)()>(&A::Foo)) << std::endl;
  16. std::cout << int(reinterpret_cast<void(*)()>(&A::Foo) == reinterpret_cast<void(*)()>(&B::Foo)) << std::endl;
  17. std::cout << int(reinterpret_cast<void(*)()>(&A::Foo) == reinterpret_cast<void(*)()>(&C::Foo)) << std::endl;
  18.  
  19. std::cout << "proper solution - works" << std::endl;
  20. std::cout << int(typeid(&A::Foo) == typeid(&A::Foo)) << std::endl;
  21. std::cout << int(typeid(&A::Foo) == typeid(&B::Foo)) << std::endl;
  22. std::cout << int(typeid(&A::Foo) == typeid(&C::Foo)) << std::endl;
  23. return 0;
  24. }
Success #stdin #stdout 0s 3340KB
stdin
Standard input is empty
stdout
naive - fails
1
1
1
needs gcc-only PMF extension - semi-works
1
0
1
proper solution - works
1
0
1