www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Setting defaults to variadic template args

reply Manu <turkeyman gmail.com> writes:
--bcaec51a7fe8f851a804cb134f8b
Content-Type: text/plain; charset=UTF-8

Is it possible?

Eg:
  struct Event(T... = (int, float))
  {
    void F(T...); // <- should default to F(int, float)
  }

Does anyone have any clever tricks that will work in this scenario? Some
magic tuple syntax?

--bcaec51a7fe8f851a804cb134f8b
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

Is it possible?<div><br></div><div>Eg:<br><div>=C2=A0 struct Event(T... =3D=
 (int, float))</div><div>=C2=A0 {</div><div>=C2=A0 =C2=A0 void F(T...); // =
&lt;- should default to F(int, float)</div><div>=C2=A0 }</div><div><br></di=
v><div>Does anyone have any clever tricks that will work in this scenario? =
Some magic tuple syntax?</div>
</div>

--bcaec51a7fe8f851a804cb134f8b--
Oct 02 2012
next sibling parent Jacob Carlborg <doob me.com> writes:
On 2012-10-02 15:15, Manu wrote:
 Is it possible?

 Eg:
    struct Event(T... = (int, float))
    {
      void F(T...); // <- should default to F(int, float)
    }

 Does anyone have any clever tricks that will work in this scenario? Some
 magic tuple syntax?

struct Event { static if (T.length == 0) void F(int, float); else void F(T); } Perhaps? -- /Jacob Carlborg
Oct 02 2012
prev sibling next sibling parent "luka8088" <luka8088 owave.net> writes:
module program;

import std.stdio;
import std.traits;
import std.typetuple;

struct Event (T...) {

   alias EraseAll!(void, TypeTuple!(T,
     Select!(T.length < 1, int, void),
     Select!(T.length < 2, float, void),
   )) T2;

   void F (T2 args) {
     writeln(typeid(typeof(args)));
   }

}

void main () {

   Event!() e1;

   e1.F(5, 6);

}


On Tuesday, 2 October 2012 at 13:15:08 UTC, Manu wrote:
 Is it possible?

 Eg:
   struct Event(T... = (int, float))
   {
     void F(T...); // <- should default to F(int, float)
   }

 Does anyone have any clever tricks that will work in this 
 scenario? Some
 magic tuple syntax?

Oct 02 2012
prev sibling next sibling parent "luka8088" <luka8088 owave.net> writes:
Or maybe... This seems like a much better solution:


module program;

import std.stdio;
import std.traits;
import std.typetuple;

template SelectTrue (bool condition, T) {
   static if (condition)
     alias T SelectTrue;
}

struct Event (T...) {

   alias TypeTuple!(T,
     SelectTrue!(T.length < 1, int),
     SelectTrue!(T.length < 2, float),
   ) T2;

   void F (T2 args) {
     writeln(typeid(typeof(args)));
   }

}

void main () {

   Event!() e1;

   e1.F(5, 6);

}



On Tuesday, 2 October 2012 at 13:44:10 UTC, luka8088 wrote:
 module program;

 import std.stdio;
 import std.traits;
 import std.typetuple;

 struct Event (T...) {

   alias EraseAll!(void, TypeTuple!(T,
     Select!(T.length < 1, int, void),
     Select!(T.length < 2, float, void),
   )) T2;

   void F (T2 args) {
     writeln(typeid(typeof(args)));
   }

 }

 void main () {

   Event!() e1;

   e1.F(5, 6);

 }


 On Tuesday, 2 October 2012 at 13:15:08 UTC, Manu wrote:
 Is it possible?

 Eg:
  struct Event(T... = (int, float))
  {
    void F(T...); // <- should default to F(int, float)
  }

 Does anyone have any clever tricks that will work in this 
 scenario? Some
 magic tuple syntax?


Oct 02 2012
prev sibling next sibling parent "luka8088" <luka8088 owave.net> writes:
And the simplest solution wins:

module program;

import std.stdio;
import std.traits;
import std.typetuple;

struct Event (T1 = int, T2 = float, Telse...) {

   alias TypeTuple!(T1, T2, Telse) T;

   void F (T args) {
     writeln(typeid(typeof(args)));
   }

}

void main () {

   Event!() e1;

   e1.F(5, 6);

}

I hope that you found the solution that you where looking for.

On Tuesday, 2 October 2012 at 13:49:56 UTC, luka8088 wrote:
 Or maybe... This seems like a much better solution:


 module program;

 import std.stdio;
 import std.traits;
 import std.typetuple;

 template SelectTrue (bool condition, T) {
   static if (condition)
     alias T SelectTrue;
 }

 struct Event (T...) {

   alias TypeTuple!(T,
     SelectTrue!(T.length < 1, int),
     SelectTrue!(T.length < 2, float),
   ) T2;

   void F (T2 args) {
     writeln(typeid(typeof(args)));
   }

 }

 void main () {

   Event!() e1;

   e1.F(5, 6);

 }



 On Tuesday, 2 October 2012 at 13:44:10 UTC, luka8088 wrote:
 module program;

 import std.stdio;
 import std.traits;
 import std.typetuple;

 struct Event (T...) {

  alias EraseAll!(void, TypeTuple!(T,
    Select!(T.length < 1, int, void),
    Select!(T.length < 2, float, void),
  )) T2;

  void F (T2 args) {
    writeln(typeid(typeof(args)));
  }

 }

 void main () {

  Event!() e1;

  e1.F(5, 6);

 }


 On Tuesday, 2 October 2012 at 13:15:08 UTC, Manu wrote:
 Is it possible?

 Eg:
 struct Event(T... = (int, float))
 {
   void F(T...); // <- should default to F(int, float)
 }

 Does anyone have any clever tricks that will work in this 
 scenario? Some
 magic tuple syntax?



Oct 02 2012
prev sibling parent Timon Gehr <timon.gehr gmx.ch> writes:
On 10/02/2012 03:15 PM, Manu wrote:
    struct Event(T... = (int, float))
    {
      void F(T...); // <- should default to F(int, float)
    }

template Event(){alias Event!(int,float) Event;} struct Event(T...){ void F(T...); }
Oct 02 2012