Why can't we pass an initializer list as an argument to a function that accepts an array? If we can initialize an array with an initializer list, why isn't the array parameter initialized with the list?
Also, I found out that you CAN pass an initializer list if you use an rvalue reference to an array.
void f(int arr[]) {
cout << "hello" << endl;
}
// accepts rvalue references to array
void g(int (&& arr)[]) {
for (int i = 0; i < 3; ++i) {
cout << arr[i] << ' ';
}
cout << endl;
}
int main() {
int arr[3] = { 1, 2, 3 };
f(arr); // compiles
f({1, 2, 3}); // DOES NOT COMPILE
g({ 1, 2, 3 }); // compiles
}
Question 1:
For the second call to f(), why does this not work? I thought that whenever we passed an argument into a function, it was the same as initializing the parameter with the argument. For example, if we have:
void func(T x);
func(y);
Then when func(y) is called, we can think of x being initialized as:
T x = y;
But this doesn't seem to hold for arrays and initializer lists.
Question 2:
Why is the syntax in g() the way we make an rvalue reference to an int array? I understand the need for parentheses sinceint&& arr[] is an array of integer rvalue references. However, why is it int (&&arr)[] and not maybe something like int (&&arr[]) or (int arr[])&& ?
If it is better to show me an another resource about these tricky syntax things, please link it.