0

GCC allowing the run-time size of array, but clang doesn't ? why ?

case : 1

int x = 5, y = x;

case : 2

int x = 3, arr[x]{2, 3, 4};
Jitu DeRaps
  • 144
  • 1
  • 9
  • I would say neither, use std::array x{2,3,4}, or std::vector x{2,3,4}. It will make passing arrays around in your code much more easy (even allows you to return arrays from functions) – Pepijn Kramer Sep 13 '22 at 05:54
  • VLA is a gcc extension. See [Expression does not evaluate to a constant](https://stackoverflow.com/questions/72239263/expression-does-not-evaluate-to-a-constant) – Jason Sep 13 '22 at 05:54
  • 1
    Using `x` as length of an array is never possible in standard C++, whether you separate the declarations or not. The size of an array must be a compile-time constant. Some compilers like GCC and Clang offer an extension to the standard language called _variable-length arrays_ originating from C, which allow runtime array sizes. But since these are language extensions there are no standardized rules on how they behave. It could be that the compilers just chose to implement it slightly differently, although Clang generally tries to emulate GCC's behavior (in most cases). – user17732522 Sep 13 '22 at 05:55

0 Answers0