Multiple return values...
Iain Buclaw
ibuclaw at ubuntu.com
Thu Mar 15 03:14:20 PDT 2012
On 15 March 2012 09:52, Derek <ddparnell at bigpond.com> wrote:
> On Thu, 15 Mar 2012 19:23:57 +1100, Timon Gehr <timon.gehr at gmx.ch> wrote:
>>>
>>> I'd like to break the nexus between science and magic here.
>>>
>>
>> // this is used to access language built-in tuples
>> template to(T...) {
>> alias T to;
>> }
>>
>> // this builds a struct akin to std.typecons.tuple
>> // it contains the function parameters as fields t[0], t[1],...
>> auto from(T...)(T t) {
>> struct Result { T t; alias t this; }
>> return Result( t );
>> }
>>
>> // to!(a,b) creates a tuple containing aliases to a and b
>>
>> to!(a,b) = from(b,a); // cannot assign Result to two fields
>> to!(a,b) = from(b,a).t; // try alias this
>> auto __tmp = from(b,a).t; // expand tuple assign to multiple assignment
>> a[0] = __tmp[0], a[1] = __tmp[1];
>
>
> Thanks, but I was hoping more for an explanation in English.
>
> Are you saying that the generated code is something like ...
>
> struct __tmpS { int Fa, int Fb };
> __tempS __tmp;
> __tmp.Fa = a;
> __tmp.Fb = b;
> a = __tmp.Fb;
> b = __tmp.Fa;
>
In effect, yes. Given that the call to from() is inlined. :-)
In GDC, you have a -fdump-tree-original switch that dumps a debug
representation (that just so happens to look C-like) of the AST of the
code to a file. You could use this to unravel some of the magic going
on under the covers. ;-)
--
Iain Buclaw
*(p < e ? p++ : p) = (c & 0x0f) + '0';
More information about the Digitalmars-d
mailing list