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.
if(false)
constraint, it'd be impossible to ever get inside it). – Veldaveleda