pointers, functions, and uniform call syntax
Artur Skawina
art.08.09 at gmail.com
Thu Sep 6 14:58:48 PDT 2012
On 09/06/12 22:07, Era Scarecrow wrote:
> On Thursday, 6 September 2012 at 12:00:05 UTC, Artur Skawina wrote:
>> On 09/06/12 13:34, Era Scarecrow wrote:
>>> Alright let's go the opposite direction. Give me an example in which passing a variable (by reference to a function) would EVER require it to check the address to see if it was null. Class/allocated objects should fail before the function gets control. ie:
>>>
>>> void func(ref int i);
>>>
>>> class X {
>>> int i;
>>> }
>>>
>>> X x;
>>> int* i;
>>> int[10] a;
>>>
>>> func(x.i); /*should fail while dereferencing x to access i,
>>> so never gets to func*/
>>> func(*i); //does this count as a lvalue? Probably not,
>>> func(a[0]);//none of these three should compile with that in mind
>>> func(0);
>>>
>>> Being named variables, and likely non-classes you are then left with mostly local variables, or arrays, or some type of pointer indirection issue. But ever case I come up with says it would fail before the function was called.
>>
>> Both '*i' and 'a[0]' count. (Even '0' could be made to work as a 'const ref' arg, but i'm not sure if that would be a good idea)
>
> I wasn't sure about *i. I can see it going either way. *i would need to be dereferenced first, a[0] would need a bounds check which then ensures it exists (even if it was dynamic); So checking an address from ref wouldn't be needed in the func.
No, '*i' does not actually dereference the pointer when used as
a ref argument.
This program won't assert
auto func(ref int i) { assert(!&i); }
void main() { int* i; func(*i); }
and would segfault if 'i' was accessed by 'func'.
artur
More information about the Digitalmars-d
mailing list