Caesar multum in plus.

Dieser Inhalt wird in Deutschland nicht angezeigt. Weitere Informationen Let's do a little C++! Let us explore the amazingly well-designed template system of C++.

Will the following code compile?

class NAT {};

class ZERO : public NAT {};

class FAIL {};

template <class C>
class SUCC : public NAT {
public:
  NAT* doWitnessing () {
    return (C*)(0xbad);
  }
  SUCC () {
    doWitnessing();
  }
};

int main (void) {
  (SUCC<SUCC<SUCC<ZERO> > > ());
}


Well, probably. But what about

class NAT {};

class ZERO : public NAT {};

class FAIL {};

template <class C>
class SUCC : public NAT {
public:
  NAT* doWitnessing () {
    return (C*)(0xbad);
  }
  SUCC () {
    doWitnessing();
  }
};

int main (void) {
  (SUCC<SUCC<SUCC<FAIL> > > ());
}


Well, hopefully not, will it? Hm. What about

class NAT {};

class ZERO : public NAT {};

class FAIL {};

template <class C>
class SUCC : public NAT {
public:
  NAT* doWitnessing () {
    return new C;
  }
  SUCC () {
    doWitnessing();
  }
};

int main (void) {
  (SUCC<SUCC<SUCC<FAIL> > > ());
}


Probably not. So how about

#include <cstdlib>
class NAT {};

class ZERO : public NAT {};

class FAIL {};

template <class C>
class SUCC : public NAT {
public:
  NAT* doWitnessing () {
    return (C*) random();
  }
  SUCC () {
    doWitnessing();
  }
};

int main (void) {
  (SUCC<SUCC<SUCC<ZERO> > > ());
}


Of course it compiles. Why shouldn't it. And so will

#include <cstdlib>
class NAT {};

class ZERO : public NAT {};

class FAIL {};

template <class C>
class SUCC : public NAT {
public:
  NAT* doWitnessing () {
    return (C*) random();
  }
  SUCC () {
    doWitnessing();
  }
};

int main (void) {
  (SUCC<SUCC<SUCC<FAIL> > > ());
}


maybe. Maybe not. How about

class NAT {};

class ZERO : public NAT {};

class FAIL {};

template <class C>
class SUCC : public NAT {
public:
  void doWitnessing () {
    if (false) NAT a = C();
  }
  SUCC () {
    doWitnessing();
  }
};

int main (void) {
  (SUCC<SUCC<SUCC<FAIL> > > ());
}


and furthermore, what about

class NAT {};

class ZERO : public NAT {};

class FAIL {};

template <class C>
class SUCC : public NAT {
public:
  void doWitnessing () {
    if (false) NAT a = C();
  }
  SUCC () {
    doWitnessing();
  }
};

int main (void) {
  (SUCC<SUCC<SUCC<ZERO> > > ());
}


Got that?