mercredi 9 octobre 2019

Why does the compiler allows non constant expression array size? [duplicate]

Can C-type arrays have non-constexpr array sizes? In the book C++ Primer, I've read that:

The dimension must be known at compile time, which means that the dimension must be a constant expression. (Page 163).

However, the following code compiles without any errors. Code is written in C++11, using the IDE Dev-C++ 5.4.0. I also tried the same code on an online compiler: Compile and Execute C++11 Online (GNU GCC v7.1.1). The results are the same.

int main(){
    int not_ci = 5;            //non-constexpr integer.
    constexpr int ci = 5;      //constexpr integer.
    int arrA[ci];              //ok, since the array size is constexpr.
    int arrB[not_ci];          //should be an error since array size must be a constexpr.

    return 0;
}

Compiles with 0 errors and 0 warnings.

Aucun commentaire:

Enregistrer un commentaire