Is the g ++ compiler to represent strings and integers differently?

I can't seem to get an array of strings declared inside a function.

void foo(string arr[], int arrSize) {
    string temp[arrSize]; //Results in error shown below
    int temp2[arrSize]; // Compiles fine
}

I get a compile-time error: a variable-length array of a non-POD element of type 'string' (aka 'basic_string, allocator>') ... [a ...

I tried to declare the arrSize parameter as a constant with void foo(string arr[], const int arrSize);and even declare a new constant integer inside the function and initialize it with arrSize, but both still produce the same error. The only way I was able to compile this was to use a magic number when declaring my array of temp strings.

Also, I know that I could just use a vector instead, but I'm just wondering why this is happening.

Thanks in advance for your help!

+4
1

, -. , g++ , POD.

, , , C, , c- ( POD), -POD-.

: A std::string - , , - ( , std::vector<int> - ints).

: , ++:

POD , .

, : " , "

+5

Source: https://habr.com/ru/post/1660097/


All Articles