`ref T` should be a type!!

Atila Neves atila.neves at gmail.com
Mon Apr 1 23:04:15 UTC 2019


On Monday, 1 April 2019 at 20:00:34 UTC, Rubn wrote:
> On Monday, 1 April 2019 at 13:57:17 UTC, Atila Neves wrote:
>> On Monday, 1 April 2019 at 13:09:28 UTC, Dein wrote:
>>> On Monday, 1 April 2019 at 01:18:44 UTC, Walter Bright wrote:
>>>> On 3/31/2019 5:35 PM, Rubn wrote:
>>>>> you can literally use it everywhere else you can use a type.
>>>>
>>>> No, you can't. An array of refs won't compile, either.
>>>>
>>>>   void test(int& a[]); // error
>>>>
>>>> A C++ ref can only appear at the top of a type AST, which is 
>>>> unlike any other type. Which exactly matches the only place 
>>>> a storage class can be!
>>>
>>> Seriously? You should know as well as I do what that function 
>>> actually translates to:
>>>
>>>    void test(int&* const a);
>>>
>>> So I ask you now, how do you get a pointer to a reference. 
>>> Its the exact same thing. Not sure if you are trying to just 
>>> deceive me with some syntax sugar in C++ or what.
>>
>> Bad example, but the point stands:
>>
>>
>> ----
>> // foo.cpp
>> int fun() {
>>     int& foo[5]; // doesn't compile
>> }
>> ----
>>
>> I'd never even thought of it until Walter mentioned it that 
>> one can't have an array of references. Huh.
>
> Arrays in C++ are just pointers. If you can't have a pointer to 
> something, it is only natural you can't have array either.

Arrays in C (and by extension C++) are most definitely not "just 
pointers". This is a common misconception due to arrays decaying 
to pointers in function calls. There are such things as array 
pointers in C, and C++ adds array references to the mix for, in 
all likelihood, consistency. I haven't met many C programmers 
that know of their existence.


>     void* ptr;   // ok
>     void arr[5]; // error
>
> So I guess Walter agrees that void isn't a type then right? You 
> can't have an array to it so that's the deciding factor.

Funnily enough your example here shows how pointers and arrays 
aren't the same thing. The reason you can't have an array of 
"voids" in C is because void has no size and the whole thing is 
nonsensical. It's a good point though that void is treated 
differently in C's type system.




More information about the Digitalmars-d mailing list