Passing struct and struct[] into a template

Jonathan M Davis via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Jul 21 23:46:01 PDT 2015


On Wednesday, July 22, 2015 04:29:23 Taylor Gronka via Digitalmars-d-learn wrote:
> I have a template function, and I want it to do something if the
> input variable is a list of structs, and something else if the
> input is a struct.

If you want a template to be different for different types, then overload it
via template constraints. e.g.

auto query(T)(string s)
    if(isDynamicArray!T)
{
}

auto query(T)(string s)
    if(!isDynamicArray!T)
{
}

You can use static if internally instead if the difference in code between
the two is small, though in most cases, unless the types are very similar,
it's cleaner to use a template constraint.

- Jonathan M Davis



More information about the Digitalmars-d-learn mailing list