This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

compile time assert

Does anyone know how to write a compiletime ASSERT?

If the condition is true, then don't generate any code. If it is false, then does #error Assert Failed to stop compilation.

Thanks,

Anh

Something like this:


if (condition)
{
}
else
{
#error Assert Failed
}

Parents
  • the code needs to run in order to see the problem.

    The code needs to run if there's no problem, too. So what's the issue? The purpose of an assertion is to make sure that the code doesn't run when the assertion is violated (which could presumably lead to all kinds of dangerous situations).

    As long as it's guaranteed that that an assertion failure causes some behaviour of the code that is impossible to mistake for a nicely running program, it has done its work. A simple run-time assert() should alwyas be enough to achieve that.

Reply
  • the code needs to run in order to see the problem.

    The code needs to run if there's no problem, too. So what's the issue? The purpose of an assertion is to make sure that the code doesn't run when the assertion is violated (which could presumably lead to all kinds of dangerous situations).

    As long as it's guaranteed that that an assertion failure causes some behaviour of the code that is impossible to mistake for a nicely running program, it has done its work. A simple run-time assert() should alwyas be enough to achieve that.

Children
No data