const, final, scope function parameters

Bruno Medeiros brunodomedeiros+spam at com.gmail
Wed May 30 07:12:46 PDT 2007


Regan Heath wrote:
> 
> 'scope' poses no performance problems as it doesn't change the existing behaviour, it is actually just a formalisation of existing behaviour.  Take this example:
> 
> class A { ..large object, lots of members.. }
> void foo(A b) {}
> A a = new A();
> foo(a);
> 
> when foo is called a copy of the reference 'a' is made and it is called 'b'.  It is 'scope' because it exists solely in the function scope, once the function returns 'b' ceases to exist (because it was a copy of 'a' on the stack and the stack is reclaimed at function exit).
> 
> This is what currently happens in D, and is what happens in C and C++ too. 'scope' is intended to detact and prevent this:
> 
> A* pa;
> void foo(A b) { pa = &b; }
> 
> In the above pa is set to the address of the reference 'b', this address becomes invalid when 'foo' returns and thus violates 'scope'.  Likewise this:
> 
> A* foo(A b) { return &b; }
> 
> will be caught and prevented.
> 
> This above code is currently valid D and the compiler gives no errors, once 'scope' is added to D the compiler will detect this and give an error.
> 

It should be noted however that it is not the value of b that is 
intrisincally 'scope'. It is the value of &b that is scope. The same 
happens with any local variable, parameter or not.


-- 
Bruno Medeiros - MSc in CS/E student
http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#D



More information about the Digitalmars-d mailing list