digitalmars.D.learn - alias of member from pointer to struct?
- BCS (7/7) Apr 13 2007 should this work?
- Jarrett Billingsley (3/10) Apr 13 2007 No. You cannot alias expressions.
- BCS (16/31) Apr 14 2007 is their a clean way to do this?
- Jarrett Billingsley (20/35) Apr 14 2007 If the static if is not at global scope, you can make a variable that po...
- BCS (2/20) Apr 14 2007 That's what I'm trying to avoid. :(
- Jarrett Billingsley (5/25) Apr 14 2007 If you want to avoid the address calculation every time you write "s.i" ...
- BCS (5/34) Apr 15 2007 In this particular case the issue is that I want to be totally sure that...
- janderson (15/52) Apr 15 2007 Not that it probably matters, but did u try this?
- BCS (24/86) Apr 16 2007 No i didn't in actual fact, the code looks more like this
- Jarrett Billingsley (11/33) Apr 16 2007 I should hope that in reality the body of go() is long enough to even
- BCS (6/16) Apr 16 2007 Yes, in fact it is. However the point is more to try and get as little r...
should this work? struct S { int i; } S* s; alias s.i foo;
Apr 13 2007
"BCS" <ao pathlink.com> wrote in message news:ce0a334393908c94bfc5c9037e4 news.digitalmars.com...should this work? struct S { int i; } S* s; alias s.i foo;No. You cannot alias expressions.
Apr 13 2007
Reply to Jarrett,"BCS" <ao pathlink.com> wrote in message news:ce0a334393908c94bfc5c9037e4 news.digitalmars.com...is their a clean way to do this? struct S { int i; int j; } S* s; static if(cond) alias s.i var; else alias s.j var; // use var for lots of things This is going to be in a performance bottle neck so it must resolve to ideal code. Also this is supposed to be demonstrating D's ability to, along with other things, remove redundancy so ideally only the actual symbol will appear in the static if.should this work? struct S { int i; } S* s; alias s.i foo;No. You cannot alias expressions.
Apr 14 2007
"BCS" <ao pathlink.com> wrote in message news:ce0a334393e38c94c9daebff03a news.digitalmars.com...is their a clean way to do this? struct S { int i; int j; } S* s; static if(cond) alias s.i var; else alias s.j var; // use var for lots of things This is going to be in a performance bottle neck so it must resolve to ideal code. Also this is supposed to be demonstrating D's ability to, along with other things, remove redundancy so ideally only the actual symbol will appear in the static if.If the static if is not at global scope, you can make a variable that points to the member: const bool pick = false; struct S { int i; int j; } void main() { S* s; static if(pick) int* var = &s.i; else int* var = &s.j; } Once macros come out you should be able to make a macro that expands to s.i or s.j.
Apr 14 2007
Reply to Jarrett,If the static if is not at global scope, you can make a variable that points to the member: const bool pick = false; struct S { int i; int j; } void main() { S* s; static if(pick) int* var = &s.i; else int* var = &s.j; }That's what I'm trying to avoid. :(
Apr 14 2007
"BCS" <ao pathlink.com> wrote in message news:ce0a334394148c94cbf4314a4da news.digitalmars.com...Reply to Jarrett,If you want to avoid the address calculation every time you write "s.i" or "s.j", that's what you'll have to do. Even a macro that expanded to "s.i" or "s.j" would not give you any performance gain.If the static if is not at global scope, you can make a variable that points to the member: const bool pick = false; struct S { int i; int j; } void main() { S* s; static if(pick) int* var = &s.i; else int* var = &s.j; }That's what I'm trying to avoid. :(
Apr 14 2007
Reply to Jarrett,"BCS" <ao pathlink.com> wrote in message news:ce0a334394148c94cbf4314a4da news.digitalmars.com...In this particular case the issue is that I want to be totally sure that exactly the same thing os done for each version and avoid the extra copy. Come to think of it though, it will probably get optimized to the extra variable version anyway. Oh well, I guess I can live with it.Reply to Jarrett,If you want to avoid the address calculation every time you write "s.i" or "s.j", that's what you'll have to do. Even a macro that expanded to "s.i" or "s.j" would not give you any performance gain.If the static if is not at global scope, you can make a variable that points to the member: const bool pick = false; struct S { int i; int j; } void main() { S* s; static if(pick) int* var = &s.i; else int* var = &s.j; }That's what I'm trying to avoid. :(
Apr 15 2007
BCS wrote:Reply to Jarrett,Not that it probably matters, but did u try this? const bool pick = false; struct S { int i; int j; static if (pick) alias i foo; else alias j foo; } S* s; ... int m = s.Foo;"BCS" <ao pathlink.com> wrote in message news:ce0a334394148c94cbf4314a4da news.digitalmars.com...In this particular case the issue is that I want to be totally sure that exactly the same thing os done for each version and avoid the extra copy. Come to think of it though, it will probably get optimized to the extra variable version anyway. Oh well, I guess I can live with it.Reply to Jarrett,If you want to avoid the address calculation every time you write "s.i" or "s.j", that's what you'll have to do. Even a macro that expanded to "s.i" or "s.j" would not give you any performance gain.If the static if is not at global scope, you can make a variable that points to the member: const bool pick = false; struct S { int i; int j; } void main() { S* s; static if(pick) int* var = &s.i; else int* var = &s.j; }That's what I'm trying to avoid. :(
Apr 15 2007
janderson wrote:BCS wrote:No i didn't in actual fact, the code looks more like this struct S { int i; int j; int go(bool pick)(S* bar) { static if (pick) { alias i foo; alias bar.j baz; } else { alias j foo; alias bar.i baz; } return foo % baz; } alias go!(true) go1; alias go!(false) go2; } It's not an exact example, but shows the problemReply to Jarrett,Not that it probably matters, but did u try this? const bool pick = false; struct S { int i; int j; static if (pick) alias i foo; else alias j foo; } S* s; .... int m = s.Foo;"BCS" <ao pathlink.com> wrote in message news:ce0a334394148c94cbf4314a4da news.digitalmars.com...In this particular case the issue is that I want to be totally sure that exactly the same thing os done for each version and avoid the extra copy. Come to think of it though, it will probably get optimized to the extra variable version anyway. Oh well, I guess I can live with it.Reply to Jarrett,If you want to avoid the address calculation every time you write "s.i" or "s.j", that's what you'll have to do. Even a macro that expanded to "s.i" or "s.j" would not give you any performance gain.If the static if is not at global scope, you can make a variable that points to the member: const bool pick = false; struct S { int i; int j; } void main() { S* s; static if(pick) int* var = &s.i; else int* var = &s.j; }That's what I'm trying to avoid. :(
Apr 16 2007
"BCS" <BCS pathlink.com> wrote in message news:f00649$j6f$2 digitalmars.com...struct S { int i; int j; int go(bool pick)(S* bar) { static if (pick) { alias i foo; alias bar.j baz; } else { alias j foo; alias bar.i baz; } return foo % baz; } alias go!(true) go1; alias go!(false) go2; } It's not an exact example, but shows the problemI should hope that in reality the body of go() is long enough to even warrant the use of such an alias. Otherwise: int go(bool pick)(S* bar) { static if (pick) return i % bar.j; else return j % bar.i; }
Apr 16 2007
Reply to Jarrett,I should hope that in reality the body of go() is long enough to even warrant the use of such an alias. Otherwise: int go(bool pick)(S* bar) { static if (pick) return i % bar.j; else return j % bar.i; }Yes, in fact it is. However the point is more to try and get as little redundancy as possible. An ideal solution would never have the same expression more than one if the second copy is in fact the exact same. I'm sort of taking to an extreme the reason that it's in a template in the first place (I don't want to wright n almost identical function).
Apr 16 2007