Reputation: 319
// since we can dedfine a static array like this
int a[5] = {0};
// decltype(a[5]) is `int [5]`
// Then how about this way?
constexpr int sum(int a, int b) { return a + b; }
int a, b;
std::cin >> a >> b;
int arr[sum(a, b)] = {0};
it can be compiled successfully, but is arr
a static array?
when I tried to print arr
type with typeid().name()
or boost::typeindex::type_id_with_cvr
, I got below error:
error: cannot create type information for type 'int [(<anonymous> + 1)]' because it involves types of variable size
std::cout << typeid(decltype(arr)).name() << std::endl;
Upvotes: 2
Views: 225
Reputation: 36379
As the value of a
and b
are not known at compile time the result of sum
is not a constexpr.
The code compiles presumably because you are using GCC which has an extension which allows declaring arrays on the stack with variable size, standard c++ doesn't allow this.
Upvotes: 6