Search code examples
c++cassertassertion

What does assert(0) mean?


I had a question like this on one of my exams and I'm still not too sure how to answer it. I understand that assertions are ways to test your program, however I'm not too sure what assert(0) is checking. Is this a trick question? It will always fail, but I don't understand why. What is it checking?

Any explanation would be great, thanks.


Solution

  • It will always fail. That's pretty much it. It will fail always for the same reason that "assert(x == 5)" will succeed whenever x = 5.

    If you're asking for an application then you would put it in code blocks that really shouldn't happen.

    switch(suit) {
      case CLUB:
      case DIAMOND:
      case HEART:
      case SPADE:
      // ...
      default:
        assert(0);
     }