Thread overview
Feature request: Templates as template parameters
Mar 23, 2008
Simen Kjaeraas
Mar 24, 2008
Simen Kjaeraas
Mar 25, 2008
Simen Kjaeraas
March 23, 2008
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
March 24, 2008
"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;


March 24, 2008
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 = 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;

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
March 24, 2008
"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.


March 25, 2008
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