WhatMeWorry píše v So 19. 09. 2015 v 17:09 +0000: > Does D provide complete template constraint granularity? > > In other words, I want to only accept structs in the template > below. > I've find the isAggregateType which is close but no cigar. Am I > missing > some other filters? > > And a more open ended question. Is there a more elegant solution > for the > below function? Maybe a one-liner? I have a knack for making > simple solutions > complex :) > > > > // Calculate the number of components for openGL generic > // vertex attribute. Must be 1, 2, 3, 4. > > // isAggregateType constraint accepts class, union, struct, and > interface > > int getNumberComponents(T)(T someStruct) if (isAggregateType!(T)) > { > int members = 0; > foreach (i, member; someStruct.tupleof) > { > //writefln("Member %s:", i); > //writefln(" type : %s", typeof(member).stringof); > //writefln(" value: %s", member); > members++; > } > return members; > }
http://dlang.org/expression.html#IsExpression 3. is ( Type == TypeSpecialization ) import std.stdio; struct S { } class C { } void f(T)(T someStruct) if (is (T == struct)) { writeln("struct"); } void f(T)(T notStruct) if (!is(T == struct)) { writeln("not a struct"); } void main() { auto c = new C(); auto s = S(); f(c); // not a struct f(s); // struct }