#include <iostream>
int main() {
int a[] = { 21, 213, 45 };
static_assert(a[22], "error for a\n");
return (0);
}
I have a lookup table shaped like an array of int, I need to check at compile time if the use of the [] operator could possibly lead to a read that is out of the scope.
I know that a static_assert needs basically 2 things:
- a constant expression
- the given expression should return a
boolor be convertible/equivalent to abool
Now in g++ 4.8.1 an initializer_list should be a constant expression in C++11, and I don't know how many times I have seen an if construct written like this if(arr[N]){...}; so what is wrong with this assert ?
I think you can generalize what you want with a little template magic.
See it in action: http://ideone.com/kj51N0