error C3863: array type 'double [length]' is not assignable

Viewed 2340

In windows MSVC, I was trying to compile the below code.

void foo(std::vector<double> &bar){
    const long int length = bar.size();
    double a[length]; //error C3863: array type 'double [length]' is not assignable
    for(int i=0; i < length; i++){
        a[i]=0.0;
    }
    //do some other things
}

the code works fine in xcode.

When I switch to MSVC, compile the code with command line:

cl /EHsc main.cpp /std:c++17

Then I have the "error C3863: array type 'double [length]' is not assignable".

Why is it different? How to get rid of the error?

1 Answers

double a[length]; is not portable C++ as variable length arrays are not implemented by the language.

std::vector<double> a(bar.size());

is the replacement. Note that the elements will be initialised to zero for you, so you can remove the initialisation loop.

Related