fork download
  1. import std.stdio, std.typecons;
  2.  
  3. /* hack for 2.042 and older compilers (February 2010)
  4.  * present on ideone
  5.  */
  6. struct My_Tuple(T...)
  7. {
  8. Tuple!T base;
  9. alias base this;
  10.  
  11. this(T args) { this.base = Tuple!T(args); }
  12. U opIndex(U)(size_t idx) { return this.field[idx]; }
  13. }
  14.  
  15. My_Tuple!T make_tuple(T...)(T args)
  16. {
  17. return typeof(return)(args);
  18. }
  19.  
  20. /* TESTING FUNCTION */
  21.  
  22. int[] foo(T...)(T args)
  23. {
  24. writeln("Called foo ...");
  25. foreach (i, arg; args)
  26. writefln(" args[%d] == %s", i, arg);
  27. writeln("... foo end");
  28.  
  29. return [ 5, 10, 15, 20 ];
  30. }
  31.  
  32. /* UNPACKER */
  33.  
  34. auto unpack(alias fun, V...)(ref My_Tuple!V t)
  35. {
  36. auto f(size_t i, U...)(U args)
  37. {
  38. static if (i == 0)
  39. return fun(args);
  40. else
  41. return f!(i - 1)(t[i - 1], args);
  42. }
  43. return f!(V.length)();
  44. }
  45.  
  46. /* main */
  47.  
  48. void main()
  49. {
  50. auto tup = make_tuple(
  51. 5, 3.14f, "hello world", [ 5, 10, 15 ], [ "foo":5, "bar":10 ]
  52. );
  53.  
  54. auto ret = unpack!foo(tup);
  55. writeln("retval from foo: ", ret);
  56. }
  57.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.d(41): Error: template prog.My_Tuple!(int,float,string,int[],int[string]).My_Tuple.opIndex(U) does not match any function template declaration
prog.d(41): Error: template prog.My_Tuple!(int,float,string,int[],int[string]).My_Tuple.opIndex(U) cannot deduce template function from argument types !()(uint)
stdout
Standard output is empty