samedi 20 décembre 2014

g++ compiles array with size given at runtime by const volatile value (not constexpr)

Can someone clarify why is this legal C++ code? (Yes, I'm asking why my code works ;) )



#include <iostream>
#include <vector>

int main()
{
const volatile std::size_t N = 10; // absolutely NOT a compile-time constant

int a[N]{}; // value-initialize it to get rid of annoying un-initialized warnings in the following line
std::cout << a[5] << std::endl; // got a zero
}


The size of the array is declared as const (NOT constexpr), still the program compiles with no warnings (-Wall, -Wextra, -Wpedantic) in g++ (but not clang++). I thought that the C++ standard explicitly specified that the size of the array should be a compile-time constant. It is absolutely not the case here.


PS: if I remove volatile, even clang++ compiles the code with no warnings.


Aucun commentaire:

Enregistrer un commentaire