Reputation: 3490
I just perceived that static_assert
is defined as a static_assert-declaration
.
By my understanding, the declaration should introduce a name, function name/object name or type name. static_assert
doesn't fit into this. This is not intuitive.
So why is static_assert
defined as a declaration instead of a postfix-expression
statement?
Follows the relevant part of the standard:
static_assert-declaration:
static_assert ( constant-expression , string-literal ) ;
Upvotes: 2
Views: 191
Reputation: 320719
Firstly, the big difference between static assertions and classic regular assert
is that assert
is an executable assertion. It is a run-time assertion that has to be executed in order to do its job. For this reason, it has to be a statement or an expression. (It happens to be an expression.) Meanwhile, static assertion works completely differently: is not an executable assertion, it is a compile-time assertion. So, it is not naturally restricted to being a statement or an expression.
Secondly, it has to be able to appear where expressions are not allowed to appear by themselves - in namespace scope and among class member declarations. In order to support such placement it has to be either a declaration or something completely new. The language authors decided not to introduce a new kind of entity and simply made it a declaration.
Thirdly, in C++11 a declaration does not necessarily introduce a name. For example, C++11 supports empty declaration and attribute declaration, neither of which introduces names. Specifically for that reason in C++11 the description of the basic concept of declaration was changed from
A declaration introduces names into a translation unit or redeclares names introduced by previous declarations. (C++03)
to
A declaration may introduce one or more names into a translation unit or redeclare names introduced by previous declarations. (C++11)
Upvotes: 6