判断符号是否为模板函数
Determining Whether a Symbol is a Template Function
我正在尝试想出一种可靠的方法来确定给定符号是否为函数模板。以下:
import std.traits: isSomeFunction;
auto ref identity(T)(auto ref T t) { return t; }
static assert(isSomeFunction!identity);
将失败,因为 identity
在实例化之前仍然是一个模板。目前我正在使用一个 hack,它依赖于 <template function symbol>.stringof
以某种方式格式化的事实:
//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);
我想知道是否有比 hacky stringof
解析更好的方法。
似乎没有比现在更好的方法在 D 中执行此操作,所以我将坚持解析 .stringof,尽管它很脏。
我正在尝试想出一种可靠的方法来确定给定符号是否为函数模板。以下:
import std.traits: isSomeFunction;
auto ref identity(T)(auto ref T t) { return t; }
static assert(isSomeFunction!identity);
将失败,因为 identity
在实例化之前仍然是一个模板。目前我正在使用一个 hack,它依赖于 <template function symbol>.stringof
以某种方式格式化的事实:
//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);
我想知道是否有比 hacky stringof
解析更好的方法。
似乎没有比现在更好的方法在 D 中执行此操作,所以我将坚持解析 .stringof,尽管它很脏。