Reputation: 1687
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.
Upvotes: 7
Views: 847
Reputation: 729
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
Upvotes: 2
Reputation: 146940
Basically, because it doesn't need it. Existing assertion mechanisms for run-time assertions are perfectly good and don't require language support.
Upvotes: 2
Reputation: 69027
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:
>>-static_assert--(--constant-expression--,--string-literal----->
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:
Libraries can detect common usage errors at compile time.
Implementations of the C++ Standard Library can detect and diagnose common usage errors, improving usability.
You can use a static_assert declaration to check important program invariants at compile time.
Upvotes: 1
Reputation: 10969
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.
Upvotes: 2
Reputation: 695
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
Upvotes: 1
Reputation: 2785
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.
Upvotes: 3
Reputation: 62975
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.
Upvotes: 1