On 26.06.2018 12:51, bol...@cylonHQ.com wrote:
> On Mon, 25 Jun 2018 19:37:59 +0300
> Paavo Helde <
myfir...@osa.pri.ee> wrote:
>> On 25.06.2018 17:13, bol...@cylonHQ.com wrote:
>>> The same way it knows how to do it in the first definition. Why would this be
>>
>>> difficult to implement?
>>
>> The first usage
>>
>> int i[] = { 1,2,3 };
>>
>> actually defines an array, not a pointer. Try
>>
>> int* i = { 1,2,3 };
>
> That might be what you have to do now, I'm asking why they DIDN'T implement
> a much simpler syntax given its already used for lvalue initialisation.
As I said, that lvalue was an array, not a pointer. If you write
void func(int x[])
then x is a pointer, which is something entirely different than an array.
So maybe your proposal is to add a special conversion rule: whenever
there is a need to convert an initializer list into a pointer, make a
hidden temporary array instead, initialize it via the initializer list
and assign the pointer to the address of the first element.
Alas, this proposal does not work with the idea that single scalars like
pointers can be also initialized via brace syntax (for "uniform"
initialization and avoiding "most vexing parse" fiasco):
int x = {2};
int y = {}; // initialized to 0
int** z = {nullptr}; // z initialized to NULL by current rules.
The new proposal would make the last z to point to the 1-element array
instead, which would change the existing behavior and the simple scalar
initialization rule. Moreover, the temporary array would be destroyed at
the semicolon, so the pointer would become dangling.
The same holds for function calls:
void foo(int** p);
foo({nullptr}); // currently passes p=NULL.
A bit saner rule would be to add special rules for [] syntax in function
parameters. Currently void foo(int* x) and void foo(int x[]) are the
same, but in principle they could mean different things.
#include<iostream>
void func1(int** b1) {std::cout << b1 << "\n";}
void func2(int* b2[]) {std::cout << b2 << "\n";}
int main() {
int** a1 = {nullptr};
int* a2[] = {nullptr};
std::cout << a1 << "\n" << a2 << "\n";
func1({nullptr});
func2({nullptr});
}
Currently this prints:
0000000000000000
000000000029F8A0
0000000000000000
0000000000000000
because func1 and func2 are identical. The new rule would make them
different so that the last line would also print non-zero.
I guess such a change could be done hypothetically, but it would
effectively change the old C rule of array decay into a pointer. I guess
all C++ specialists are so familiar with that rule that it has never
occurred to them to change it, and probably they are right, changing
something so low-level probably has bad consequences.