Search code examples
template-meta-programmingd

Determining Whether a Symbol is a Template Function


I'm trying to come up with a robust way of determining whether a given symbol is a function template. The following:

import std.traits: isSomeFunction;

auto ref identity(T)(auto ref T t) { return t; }

static assert(isSomeFunction!identity);

Will fail as identity is still a template until it is instantiated. Currently I am using a hack that relies on the fact that <template function symbol>.stringof is formatted a certain way:

//ex: f.stringof == identity(T)(auto ref T t)
template isTemplateFunction(alias f)
{
    import std.algorithm: balancedParens, among;

    enum isTemplateFunction = __traits(isTemplate, f) 
        && f.stringof.balancedParens('(', ')') 
        && f.stringof.count('(') == 2 
        && f.stringof.count(')') == 2;
}

//Passes
static assert(isTemplateFunction!identity);

I'd like to know if there's a better way to do this other than hacky stringof parsing.


Solution

  • It seems there's no better way to do this in D as it is now, so I will stick to parsing .stringof, as dirty a hack as it is.