Puzzle: To avoid type checking

I formed a good interview question by accident. :)

template<typename T> bool foo (T obj) { if(typeid(T) == typeid(obj)) return false; return true; // <-- execute this } 

You should call (only above) foo() so that it returns true . Conditions:

  • Cannot edit or reload foo() or typeid
  • Allowed to use specific platform hackers
  • No #define allowed
+6
source share
2 answers
 int main () { typedef char C[1]; foo<C>(0); // returns true; } 

Refer to this question for an explanation of this answer and the root of this question.

+2
source
 #include <cassert> struct B { virtual ~B() {} }; int main() { struct : B {} x; assert(foo<B&>(x)); } 

The action is there .

+11
source

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


All Articles