www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Verify tuple is a tuple of class objects?

reply Sean Eskapp <eatingstaples gmail.com> writes:
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?
Feb 18 2011
next sibling parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
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
Feb 18 2011
parent Stewart Gordon <smjg_1998 yahoo.com> writes:
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.
Feb 18 2011
prev sibling next sibling parent reply bearophile <bearophileHUGS lycos.com> writes:
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
Feb 18 2011
parent "Simen kjaeraas" <simen.kjaras gmail.com> writes:
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
Feb 18 2011
prev sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
Can anyone explain to me why this throws:

class Foo() { }
void main()
{
    static if (is(Foo == class))
    {
    }
    else
    {
        static assert(0);
    }
}
Feb 18 2011