On Thursday, 6 September 2012 at 21:58:43 UTC, Artur Skawina
wrote:
This program won't assert
and would segfault if 'i' was accessed by 'func'.
So it seems. The way I read it says it dereferences it first.
Regardless that you are forcibly referencing a pointer which is
low-level trickery; which I'm sure is undefined behavior.
Course if you have to check/treat it as a pointer, adding @safe
suddenly refuses to compile (Won't let you get the address); But
if you leave it out (without @trusted either) and make main
@safe, suddenly that doesn't compile either. (Because I'm sure
quite a few functions you'd want to share will end up being @safe
and pure).
auto func(ref int i) @safe {
assert(!&i); //won't compile as @safe
debug { //ignore that writeln is system
writeln("Address:", &i); //here too
writeln("Value:", i);
}
return i;
}
void main() @safe {
int v = 100; func(v);
int* i; func(*i);
}
As I commented before: Would you really want to blindly put
@trusted on everything? In order for the above to work, either
neither is @safe, or func is @trusted.
Course you can always leave @safe out, assuming you aren't
making anything you intend to share or will never be called by
anything that's @safe.