2

Is there a way to zero init an array of any type with a variable size? So what I want is to apply something like this:

int results[5] = {};

to a template like this:

T results[i] = {};

When I try this, my compiler says this: 'results' declared as array of functions of type 'T ()'. When I use any fixed typename I get this error: Variable-sized object may not be initialized

Also i is a variable that is passed in when this function is called.

user11914177
  • 885
  • 11
  • 33
  • 4
    Variable length arrays are not supported by C++ standard. Array size must be known during compilation. So, am not sure what you are asking about. – Algirdas Preidžius Dec 19 '19 at 21:10
  • 3
    `std::vector results(i);` – NathanOliver Dec 19 '19 at 21:11
  • @AlgirdasPreidžius I don't know what you mean, I used `T results[i];` without any problem... – user11914177 Dec 19 '19 at 21:11
  • If you are using gcc as your compiler, add `-pedantic` to your compiler options. – NathanOliver Dec 19 '19 at 21:12
  • @NathanOliver-ReinstateMonica I'm using clang++ but it doesn't work... – user11914177 Dec 19 '19 at 21:14
  • @user11914177 1) "_I don't know what you mean_" I don't understand what isn't clear about "Variable length arrays are not supported by C++ standard" 2) "_I used T results[i]; without any problem_" Please show [mre], showing this. For example, if you use it in a templated function/type, and don't instantiate such template anywhere - such code might not even be compiled. Other possibility is: some compilers allow VLAs by an extension. But it's still, not part of standard C++. – Algirdas Preidžius Dec 19 '19 at 21:15
  • 2
    @user11914177 That's the point. `T results[i]` is a run time sized array and those are not allowed in C++. clang and gcc by default have an extension that lets you do it, but it is non-standard behavior. By using `-pedantic` you turn that feature off and tell the compiler to strictly follow the C++ standard. – NathanOliver Dec 19 '19 at 21:16
  • @AlgirdasPreidžius Obviously I understand what this means, but I as surprised so see it working when there isn't an initializing list. This is the minimal reproducible example... – user11914177 Dec 19 '19 at 21:16
  • @NathanOliver-ReinstateMonica oh now it is clear. – user11914177 Dec 19 '19 at 21:18
  • @Nathan "By using -pedantic you turn that feature off and tell the compiler to strictly follow the C++ standard." - no need for `-pedantic`, just ask for the standard compliant version of the language with `-std=C++17` rather than the default `gnu-...` variant that allows extensions. – Jesper Juhl Dec 19 '19 at 21:30
  • @JesperJuhl Even `-std=c++17` doesn't work: http://coliru.stacked-crooked.com/a/c1daac5bae31b6c4 – NathanOliver Dec 19 '19 at 21:30

1 Answers1

2

Variable-length arrays are non-standard in C++, and your compiler supports them as an extension. Your compiler also told you the limitations of the extension: the variable-length array may not have an initializer. As a work-around, you could do this:

int results[i];
std::fill(results, results + i, 0);

Of course, the better way is to use std::vector so you are not relying on any non-standard extensions.

Brian Bi
  • 111,498
  • 10
  • 176
  • 312