digitalmars.D.learn - alias of member from pointer to struct?
- BCS <ao pathlink.com> Apr 13 2007
- "Jarrett Billingsley" <kb3ctd2 yahoo.com> Apr 13 2007
- BCS <ao pathlink.com> Apr 14 2007
- "Jarrett Billingsley" <kb3ctd2 yahoo.com> Apr 14 2007
- BCS <ao pathlink.com> Apr 14 2007
- "Jarrett Billingsley" <kb3ctd2 yahoo.com> Apr 14 2007
- BCS <ao pathlink.com> Apr 15 2007
- janderson <askme me.com> Apr 15 2007
- BCS <BCS pathlink.com> Apr 16 2007
- "Jarrett Billingsley" <kb3ctd2 yahoo.com> Apr 16 2007
- BCS <ao pathlink.com> Apr 16 2007
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...should this work? struct S { int i; } S* s; alias s.i foo;
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.
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 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. :(
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.
Apr 14 2007
Reply to Jarrett,"BCS" <ao pathlink.com> wrote in message news:ce0a334394148c94cbf4314a4da news.digitalmars.com...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; }
"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.
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.
Apr 15 2007
BCS wrote:Reply to Jarrett,"BCS" <ao pathlink.com> wrote in message news:ce0a334394148c94cbf4314a4da news.digitalmars.com...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; }
"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.
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.
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;
Apr 15 2007
janderson wrote:BCS wrote:Reply to Jarrett,"BCS" <ao pathlink.com> wrote in message news:ce0a334394148c94cbf4314a4da news.digitalmars.com...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. :(
"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.
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.
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;
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 problem
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 problem
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; }
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








BCS <ao pathlink.com>