我正在尝试提出一种确定给定符号是否为函数模板的可靠方法。下列:
import std.traits: isSomeFunction;
auto ref identity(T)(auto ref T t) { return t; }
static assert(isSomeFunction!identity);
将失败为
identity
在实例化之前仍然是一个模板。目前我正在使用一个依赖于 <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,尽管它是一个肮脏的黑客。
关于template-meta-programming - 确定一个符号是否是一个模板函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33043839/