我正试图提出一种确定给定符号是否为函数模板的强大方法。如下:
import std.traits: isSomeFunction;
auto ref identity(T)(auto ref T t) { return t; }
static assert(isSomeFunction!identity);
将失败,因为identity
在实例化之前仍然是模板。目前,我正在使用一种依赖于<template function symbol>.stringof
以某种方式格式化的事实的hack:
//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);
我想知道是否有更好的方法来做到这一点,而不是黑客stringof
解析。
现在在D中似乎没有更好的方法来做这个,所以我将坚持解析。stringof,尽管它是一个肮脏的hack