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};
3
  • I would say neither, use std::array<int,3> 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) Commented Sep 13, 2022 at 5:54
  • VLA is a gcc extension. See Expression does not evaluate to a constant Commented Sep 13, 2022 at 5: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). Commented Sep 13, 2022 at 5:55

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.