www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Feature request: Templates as template parameters

reply "Simen Kjaeraas" <simen.kjaras gmail.com> writes:
struct foo(T =3D void)
{
     static if (!is(T =3D=3D void))
     {
         mixin T;
     }
}

I have a struct basically looking like this, and instantiated with T bei=
ng  =

a
template to be mixin'd in the struct (other methods are added via CTFE'd=
  =

string
mixins and a few are inline).

As you an see, the line 'static if (!is(T =3D=3D void))' is hardly safe,=
 and  =

should
be exchanged for 'static if (is(T =3D=3D template))' or the parameter li=
st with
something like 'foo(T : template)'. Could we get something like this?

--Simen
Mar 23 2008
parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Simen Kjaeraas" <simen.kjaras gmail.com> wrote in message 
news:op.t8hkl3ln1hx7vj spill04.lan...
struct foo(T = void)
{
     static if (!is(T == void))
     {
         mixin T;
     }
}

I have a struct basically looking like this, and instantiated with T being
a
template to be mixin'd in the struct (other methods are added via CTFE'd
string
mixins and a few are inline).

As you an see, the line 'static if (!is(T == void))' is hardly safe, and
should
be exchanged for 'static if (is(T == template))' or the parameter list with
something like 'foo(T : template)'. Could we get something like this?

--Simen

Templates are not types, and you cannot instantiate a template using a 
template as a type parameter.  In order to pass a template to another 
template, you use an alias parameter:

struct foo(alias T)
{
    mixin T;
}

template bar()
{
    int x;
}

alias foo!(bar) baz; 
Mar 23 2008
parent reply "Simen Kjaeraas" <simen.kjaras gmail.com> writes:
On Mon, 24 Mar 2008 01:07:43 +0100, Jarrett Billingsley  =

<kb3ctd2 yahoo.com> wrote:

 "Simen Kjaeraas" <simen.kjaras gmail.com> wrote in message
 news:op.t8hkl3ln1hx7vj spill04.lan...
 struct foo(T =3D void)
 {
      static if (!is(T =3D=3D void))
      {
          mixin T;
      }
 }

 I have a struct basically looking like this, and instantiated with T  =
 being
 a
 template to be mixin'd in the struct (other methods are added via CTFE=
'd
 string
 mixins and a few are inline).

 As you an see, the line 'static if (!is(T =3D=3D void))' is hardly saf=
e, and
 should
 be exchanged for 'static if (is(T =3D=3D template))' or the parameter =
list =
 with
 something like 'foo(T : template)'. Could we get something like this?

 --Simen

 Templates are not types, and you cannot instantiate a template using a=
 template as a type parameter.  In order to pass a template to another
 template, you use an alias parameter:

 struct foo(alias T)
 {
     mixin T;
 }

 template bar()
 {
     int x;
 }

 alias foo!(bar) baz;
You are right, of course. New question, then: How do I check if a templa= te = alias parameter is a template? T.stringof does give me some information, perhaps it is enough to check = = that it is on the form .*\(.*\). -- Simen
Mar 24 2008
parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Simen Kjaeraas" <simen.kjaras gmail.com> wrote in message 
news:op.t8jdb1dg1hx7vj spill04...
On Mon, 24 Mar 2008 01:07:43 +0100, Jarrett Billingsley
<kb3ctd2 yahoo.com> wrote:

You are right, of course. New question, then: How do I check if a template
alias parameter is a template?
T.stringof does give me some information, perhaps it is enough to check
that it is on the form .*\(.*\).

-- Simen

I'm not sure that you can.  It'll just fail if you try to instantiate/mix it 
in if it's not a template, though you'll probably get a less-than-helpful 
error message in that case. 
Mar 24 2008
parent "Simen Kjaeraas" <simen.kjaras gmail.com> writes:
On Tue, 25 Mar 2008 00:59:16 +0100, Jarrett Billingsley  
<kb3ctd2 yahoo.com> wrote:

 "Simen Kjaeraas" <simen.kjaras gmail.com> wrote in message
 news:op.t8jdb1dg1hx7vj spill04...
 On Mon, 24 Mar 2008 01:07:43 +0100, Jarrett Billingsley
 <kb3ctd2 yahoo.com> wrote:

 You are right, of course. New question, then: How do I check if a  
 template
 alias parameter is a template?
 T.stringof does give me some information, perhaps it is enough to check
 that it is on the form .*\(.*\).

 -- Simen

 I'm not sure that you can.  It'll just fail if you try to  
 instantiate/mix it
 in if it's not a template, though you'll probably get a less-than-helpful
 error message in that case.
Exactly. Especially when it is hidden behind another template for more prettiful syntax. Hence template foo(alias T : template) would be a Good Thing(tm), imho. -- Simen
Mar 24 2008