Puzzle: To escape the check of typeid
Domanda
I formed a nice interview question by chance. :)
template<typename T>
bool foo (T obj)
{
if(typeid(T) == typeid(obj))
return false;
return true; // <-- execute this
}
You have to call (only above mentioned) foo()
in such a way that it returns true
. Conditions are,
- Cannot edit or overload
foo()
ortypeid
- No platform specific hacks allowed
- No
#define
allowed
Soluzione 2
int main ()
{
typedef char C[1];
foo<C>(0); // returns true;
}
Refer this question to know the explanation of this answer and root of this question.
Altri suggerimenti
#include <cassert>
struct B { virtual ~B() {} };
int main()
{
struct : B {} x;
assert(foo<B&>(x));
}
Action is over there.
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow