Thread overview
Verify tuple is a tuple of class objects?
Feb 18, 2011
Sean Eskapp
Feb 19, 2011
Stewart Gordon
Feb 18, 2011
bearophile
Feb 18, 2011
Simen kjaeraas
Feb 18, 2011
Andrej Mitrovic
February 18, 2011
Is there a way to "run" a template at compile time, without using a member? What I'm trying to do is verify that every element of a tuple is a class type, and so far, I've been doing this:

template VerifyTuple(Type, Types...)
{
	static assert(is(Type : Object), Type.stringof ~ " is not a class type.");

	static if(Types.length == 0)
		alias void dummy;
	else
		alias VerifyTuple!(Types).dummy dummy;
}

and to use it, I have to do this:
class Foo(T...)
{
    alias VerifyTuple!(T).dummy dummy;
}

Is there any way to just "run" the template, without bothering to use the dummy aliases?
February 18, 2011
On Fri, 18 Feb 2011 16:15:16 -0500, Sean Eskapp <eatingstaples@gmail.com> wrote:

> Is there a way to "run" a template at compile time, without using a member?
> What I'm trying to do is verify that every element of a tuple is a class type,
> and so far, I've been doing this:
>
> template VerifyTuple(Type, Types...)
> {
> 	static assert(is(Type : Object), Type.stringof ~ " is not a class type.");
>
> 	static if(Types.length == 0)
> 		alias void dummy;
> 	else
> 		alias VerifyTuple!(Types).dummy dummy;
> }
>
> and to use it, I have to do this:
> class Foo(T...)
> {
>     alias VerifyTuple!(T).dummy dummy;
> }
>
> Is there any way to just "run" the template, without bothering to use the
> dummy aliases?

eponymous should help (also cleaned up some of your code):

template VerifyTuple(Types...)
{
   static if(Types.length == 0)
      enum bool VerifyTuple = true;
   else
      enum bool VerifyTuple == is(Type : Object) && VerifyTuple!(Types[1..$]);
}

class Foo(T...)
{
   static assert(VerifyTuple!(T...), "one of types in " ~ T.stringof ~ " is not a class");
}

It doesn't identify the specific type that isn't a class, but that could be done with a separate template.

-Steve
February 18, 2011
Sean Eskapp:

> What I'm trying to do is verify that every element of a tuple is a class type,

If you mean a TypeTuple, this is a solution:

import std.typetuple: allSatisfy, TypeTuple;

template IsClass(T) {
    enum IsClass = is(T == class);
}

class Foo {}
class Bar {}
struct Spam {}

alias TypeTuple!(Foo, Bar, Spam) T1;
alias TypeTuple!(Foo, Bar, Foo) T2;

static assert(!allSatisfy!(IsClass, T1));
static assert(allSatisfy!(IsClass, T2));

void main() {}

I don't know if there is a way to write IsClass() in a shorter way, like a "lambda template".

Bye,
bearophile
February 18, 2011
Can anyone explain to me why this throws:

class Foo() { }
void main()
{
    static if (is(Foo == class))
    {
    }
    else
    {
        static assert(0);
    }
}
February 18, 2011
bearophile <bearophileHUGS@lycos.com> wrote:

> I don't know if there is a way to write IsClass() in a shorter way, like a "lambda template".

No such thing, sadly. I have suggested it before, and would love to see
such a feature.

-- 
Simen
February 19, 2011
On 18/02/2011 21:22, Steven Schveighoffer wrote:
<snip>
> template VerifyTuple(Types...)
> {
> static if(Types.length == 0)
> enum bool VerifyTuple = true;
> else
> enum bool VerifyTuple == is(Type : Object) && VerifyTuple!(Types[1..$]);
<snip>

You have two typos there.  Corrected version:

    enum bool VerifyTuple = is(Types[0] : Object) && VerifyTuple!(Types[1..$]);

But perhaps even nicer:

----------
template VerifyTuple() {
    enum bool VerifyTuple = true;
}

template VerifyTuple(T, Ypes...) {
    enum bool VerifyTuple = is(T : Object) && VerifyTuple!(Ypes);
}
----------

(Some of you will be able to guess what other language I've programmed in in my time from this.)

Stewart.