Take the 2-minute tour ×

Now "static_assert" is a keyword in C++0x I thought it would be logical to replace the C "assert" macro with an "assert" keyword too.

share|improve this question

7 Answers 7

static_assert is interpreted at compile time so it has to be a keyword so the compiler can process it.

assert doesn't need to be a keyword, and it doesn't make much sense to make it one, since there are many ways a program might want to respond to assertion success or failure. Therefore, it makes more sense to implement it in a library, and it is typically implemented as a macro.

share|improve this answer
There being multiple ways to respond to a failed assertion is not much of an argument, since even without being a keyword the language specifies behavior –  Dennis Zickefoose Jun 2 '11 at 19:21
@Dennis: It specifies behaviour if you use the assert defined in <cassert>; that doesn't stop you from providing your own definition. –  Oliver Charlesworth Jun 3 '11 at 8:30
@Oli: if you include any standard library header, you can not provide your own implementation of assert. So for most practical purposes, the language specifies the behavior same as if it were a proper keyword. –  Dennis Zickefoose Jun 3 '11 at 16:14
You can redefine the assert (or any other) macro. You can't redefine a keyword. –  Lawrence McAlpin Jun 3 '11 at 19:38
Only if you do not include any standard library headers in that particular translation unit. Once you do so, all names found in all standard library headers are off limits. "A translation unit that includes a standard library header shall not #define or #undef names declared in any standard library header." –  Dennis Zickefoose Jun 7 '11 at 19:02

This can not be done for compatibility with the code already written in c which has assert as a variable name. And hence as oli mentioned we won't be able to compile as assert is no longer macro

share|improve this answer
C doesn't have assert as a variable name. It's a macro in C as well. –  MSalters Jun 3 '11 at 8:15

assert has no compile time meaning, except during Pre-processing. The preprocessor has no knowledge of the C++ language, so a keyword makes no sense.

By contrast, static_assert is evaluated at compile time. Making it a keyword makes more sense in that regard. The compiler cares about it's existence.

There are also historic reasons; it was not a keyword in C, and making it one in C++ would have rendered existing assert macros result in undefined behavior.

share|improve this answer

Basically, because it doesn't need it. Existing assertion mechanisms for run-time assertions are perfectly good and don't require language support.

share|improve this answer

The other answers give some possible answers to your question, but a recent proposal indicates that assert may indeed become a keyword in C++17: https://isocpp.org/files/papers/N4154.pdf

share|improve this answer
Thanks for the link. I agree with the author that assert should be an operator. I especially like the ability to provide a custom diagnostic message similar to static_assert. –  Ricky65 Nov 2 at 14:08

In C++0x (from here):

In C++0x, static assertions can be declared to detect and diagnose common usage errors at compile time.

this is static_assert syntax:


where constant-expression must be contextually converted to bool. If it converts to false, then the compiler will emit an error according the string-literal.

So, this is basically an extension of the language that needs a keyword. It is not a runtime mechanism.

Again from the document linked above:

The addition of static assertions to the C++ language has the following benefits:

  1. Libraries can detect common usage errors at compile time.

  2. Implementations of the C++ Standard Library can detect and diagnose common usage errors, improving usability.

  3. You can use a static_assert declaration to check important program invariants at compile time.

share|improve this answer

assert can be implemented in a library, static_assert cannot. So static_assert gets a keyword because it needs language support, and assert doesn't.

share|improve this answer
static assert can and has been implemented in a library: boost.org/doc/libs/1_46_1/doc/html/boost_staticassert.html –  Ferruccio Jun 2 '11 at 18:10
@Ferrucio: That's not the same as having it in the language. You could make the same case about lambdas, but the quality is definitely not as high. –  Puppy Jun 2 '11 at 18:13
@Ferruccio : The error messages from library-based static_assert implementations tend to be the sort of thing that gives C++ a bad name. Language-based static_assert allows for very clean, understandable errors. –  ildjarn Jun 2 '11 at 18:19
@DeadMG & ildjam: I agree on both points, but that doesn't mean that it can't be implemented in a library. –  Ferruccio Jun 2 '11 at 18:43
@Ferruccio : Essentially a static_assert is just a compiler error, so yes, causing a compiler error in a library is possible. :-P –  ildjarn Jun 2 '11 at 18:51

Your Answer


