alias of member from pointer to struct?

BCS BCS at pathlink.com
Mon Apr 16 08:49:10 PDT 2007


janderson wrote:
> BCS wrote:
> 
>> Reply to Jarrett,
>>
>>> "BCS" <ao at pathlink.com> wrote in message
>>> news:ce0a334394148c94cbf4314a4da at 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.
>>>
>>
>> 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


More information about the Digitalmars-d-learn mailing list