Struct should be invalid after move

sanjayss dummy at dummy.dummy
Wed Nov 28 20:52:14 UTC 2018


On Wednesday, 28 November 2018 at 16:58:32 UTC, H. S. Teoh wrote:
> Yeah, this seems to be a not-very-well-known aspect of syntax 
> common across C, C++, Java, and D:
>
> 	ReturnType myFunc(Args args) {
> 		int var1;
> 		someCode();
> 		{
> 			int var2;
> 			someOtherCode();
> 		}
> 		// var2 no longer in scope, but var1 still is.
> 		{
> 			// Can reuse identifier 'var2' without conflict
> 			float var2;
> 			yetMoreCode();
> 		}
> 		etcetera();
> 	}
>
> It's a very useful construct in ensuring that temporaries don't 
> last longer than they ought to. (Syntactically anyway... the 
> compiler may or may not actually translate that directly in the 
> executable. But the point is to avoid programmer slip-ups.)
>
>
> T

Yes, you can get what I asked for using blocks, but there are use 
cases where you can't always use blocks effectively. Also there 
is the aesthetic aspect -- all the curly braces and indentation 
makes code annoying to read.


More information about the Digitalmars-d mailing list