Re: Setting defaults to variadic template args

2012-10-02 Thread Jacob Carlborg

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


Re: Setting defaults to variadic template args

2012-10-02 Thread luka8088


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?





Re: Setting defaults to variadic template args

2012-10-02 Thread Manu
On 2 October 2012 16:24, Andrej Mitrovic andrej.mitrov...@gmail.com wrote:

 On 10/2/12, Manu turkey...@gmail.com wrote:
  Does anyone have any clever tricks that will work in this scenario? Some
  magic tuple syntax?

 Without resorting to helper templates I only know of using this internal
 trick:

 struct Event(Args...)
 {
 static if (Args.length)
 alias Args T;
 else
 alias TypeTuple!(int, float) T;

 void F(T t) { }
 }

 Also there's a somewhat related bug but for variadic template
 functions with default arguments:
 http://d.puremagic.com/issues/show_bug.cgi?id=8687


I think this is fine for my needs. Thanks!


Re: Setting defaults to variadic template args

2012-10-02 Thread luka8088

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?





Re: Setting defaults to variadic template args

2012-10-02 Thread luka8088

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?





Re: Setting defaults to variadic template args

2012-10-02 Thread Manu
On 2 October 2012 16:45, Manu turkey...@gmail.com wrote:

 On 2 October 2012 16:24, Andrej Mitrovic andrej.mitrov...@gmail.comwrote:

 On 10/2/12, Manu turkey...@gmail.com wrote:
  Does anyone have any clever tricks that will work in this scenario? Some
  magic tuple syntax?

 Without resorting to helper templates I only know of using this internal
 trick:

 struct Event(Args...)
 {
 static if (Args.length)
 alias Args T;
 else
 alias TypeTuple!(int, float) T;

 void F(T t) { }
 }

 Also there's a somewhat related bug but for variadic template
 functions with default arguments:
 http://d.puremagic.com/issues/show_bug.cgi?id=8687


 I think this is fine for my needs. Thanks!


Actually, this leaves a problem...
Distinction between:
  Event x; // using defaults
and
  Event!() x; // explicitly selecting none (which would fall back to the
defaults)

Is such a distinction possible? I presume not...


Re: Setting defaults to variadic template args

2012-10-02 Thread Andrej Mitrovic
On 10/2/12, Manu turkey...@gmail.com wrote:
 Actually, this leaves a problem...
 Distinction between:
   Event x; // using defaults
 and
   Event!() x; // explicitly selecting none (which would fall back to the
 defaults)

 Is such a distinction possible? I presume not...


Well actually you can't use the first syntax at all with templates or
templated types. :/


Re: Setting defaults to variadic template args

2012-10-02 Thread Manu
On 2 October 2012 18:56, Andrej Mitrovic andrej.mitrov...@gmail.com wrote:

 On 10/2/12, Manu turkey...@gmail.com wrote:
  Actually, this leaves a problem...
  Distinction between:
Event x; // using defaults
  and
Event!() x; // explicitly selecting none (which would fall back to the
  defaults)
 
  Is such a distinction possible? I presume not...
 

 Well actually you can't use the first syntax at all with templates or
 templated types. :/


Errr, really? If the template has default args, surely you don't need to
specify any template args?
I'm sure I've done that before... :/


Re: Setting defaults to variadic template args

2012-10-02 Thread Andrej Mitrovic
On 10/2/12, Manu turkey...@gmail.com wrote:
 Are you sure it's not fixed? I'm sure I do it all the time... :/
 Or I'm smoking some really good stuff.

I'd really like to see how you do it. If not, I'll have some of that
stuff you're having, thanks. :p


Re: Setting defaults to variadic template args

2012-10-02 Thread Jacob Carlborg

On 2012-10-02 18:10, Manu wrote:


Are you sure it's not fixed? I'm sure I do it all the time... :/
Or I'm smoking some really good stuff.


It's not needed for functions templates. But it is needed for type 
templates.


--
/Jacob Carlborg


Re: Setting defaults to variadic template args

2012-10-02 Thread Timon Gehr

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...);
}