Re: Local static variables must have unique names within a function's scope.

2018-01-19 Thread Jonathan M Davis via Digitalmars-d-learn
On Friday, January 19, 2018 10:11:36 H. S. Teoh via Digitalmars-d-learn wrote: > Fortunately this is not the case. However, the static variable case is > annoying, and it's actually one case of a larger problem: > > void main() { > foreach (i; 0 .. 10) { > struct S { >

Re: Local static variables must have unique names within a function's scope.

2018-01-19 Thread Steven Schveighoffer via Digitalmars-d-learn
On 1/19/18 1:11 PM, H. S. Teoh wrote: Fortunately this is not the case. However, the static variable case is annoying, and it's actually one case of a larger problem: void main() { foreach (i; 0 .. 10) { struct S {

Re: Local static variables must have unique names within a function's scope.

2018-01-19 Thread H. S. Teoh via Digitalmars-d-learn
On Fri, Jan 19, 2018 at 02:16:24PM +, tipdbmp via Digitalmars-d-learn wrote: > > Mostly, it's just a bad idea - it's very easy for a person reading > > the code after you've written it to get the two x's mixed up. > > // example from: 19.17.1.3 > void main() > { > { static int x; } >

Re: Local static variables must have unique names within a function's scope.

2018-01-19 Thread tipdbmp via Digitalmars-d-learn
Mostly, it's just a bad idea - it's very easy for a person reading the code after you've written it to get the two x's mixed up. // example from: 19.17.1.3 void main() { { static int x; } { static int x; } // error { int i; } { int i; } // ok } I don't really see how the

Re: Local static variables must have unique names within a function's scope.

2018-01-19 Thread Simen Kjærås via Digitalmars-d-learn
On Friday, 19 January 2018 at 11:02:01 UTC, tipdbmp wrote: The following seems to work in C++, but errors in D, why is that? int foo(int* num) { { static int x = 10; x += 1; *num += x; } { static int x = 20; // error: foo.x is already defined in

Local static variables must have unique names within a function's scope.

2018-01-19 Thread tipdbmp via Digitalmars-d-learn
The following seems to work in C++, but errors in D, why is that? int foo(int* num) { { static int x = 10; x += 1; *num += x; } { static int x = 20; // error: foo.x is already defined in another scope in foo x += 2; *num += x; }