ref tuples

Artur Skawina art.08.09 at gmail.com
Wed Jul 3 09:35:01 PDT 2013


On 07/03/13 18:29, Brad Anderson wrote:
> On Wednesday, 3 July 2013 at 11:54:39 UTC, Artur Skawina wrote:
>> On 07/03/13 02:22, Brad Anderson wrote:
>>> C++11's std::tuple includes a function std::tie that takes references to the arguments and returns a tuple that maintains the references to the arguments.
>>>
>>> Along with the usual cases where you'd want reference semantics it also enables this interesting construct for unpacking tuples.
>>>
>>> int a, b;
>>> tie(a, b) = make_tuple(1, 2);
>>>
>>> assert(a == 1 && b == 2);
>>>
>>> Is there any way to do something similar with std.typecons.Tuple?
>>
>> Well, aliases can be used to get a similar effect.
>>
>>    template tie(A...) { alias tie = A; }
>>    tie!(a, b) = tuple(1, 2);
> 
> That won't work.  a and b aren't held as references (also you passed them as type parameters :P).

Try it...

And, yes, the fact that 'A...' template parms accept symbols
is not exactly obvious. But it's much more useful that way.

artur


More information about the Digitalmars-d-learn mailing list