Passing a single tuple or multiple values
John via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Tue Jul 19 00:23:52 PDT 2016
On Tuesday, 19 July 2016 at 01:22:01 UTC, jmh530 wrote:
> import std.typecons : isTuple, tuple;
> import std.stdio : writeln;
>
> auto foo(T...)(T x)
> {
> T[0] y;
> foreach (i, e; x)
> {
> y += e;
> }
> return y;
> }
>
> auto bar(T)(T x)
> {
> static if (isTuple!T)
> {
> return foo(x.expand);
> }
> }
>
> auto bar(T...)(T x)
> {
> return foo(x);
> }
auto bar(T...)(T x)
{
static if (T.length == 1 && isTuple!(T[0]))
return foo(x.expand);
else
return foo(x);
}
>
> void main()
> {
> auto x = tuple(1, 2);
> auto y = bar(x);
> auto z = bar(x.expand);
> writeln(y);
> writeln(z);
> }
More information about the Digitalmars-d-learn
mailing list