سؤال

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,

  1. Cannot edit or overload foo() or typeid
  2. No platform specific hacks allowed
  3. No #define allowed
هل كانت مفيدة؟

المحلول 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.

نصائح أخرى

#include <cassert>

struct B { virtual ~B() {} };

int main()
{
    struct : B {} x;
    assert(foo<B&>(x));
}

Action is over there.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top