Why does this compile?

closed account (EwCjE3v7)
Why does this compile? Value isn`t known till run time? What if this was to return a variable that was not a constexpr?

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
using std::cout; using std::endl; using std::cin;

int txt_size()
{
    return 0;
}
int main()
{
    int ia[txt_size()];
    return 0;
}
it doesn't compile at least not on my compiler. Must be a quirk of the compiler you are using.
That value is known at compile-time, if the compiler is aggressive enough in optimization. Depends on compiler.

Similarly, template metaprogramming could do rather complex calculations.

A support for VLA (variable lenght arrays) would allow runtime size determination. C has that. Next C++ standard might get it too (whether it is sane or not).
Topic archived. No new replies allowed.