template-meta-programming - 确定一个符号是否是一个模板函数

标签 template-meta-programming d

我正在尝试提出一种确定给定符号是否为函数模板的可靠方法。下列:

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/

相关文章:

c++ - C++ 模板中的模板参数

linq - 范围投影: equivalent of LINQ select in D?

d - 结构和元组有什么原则区别?

c++ - 在 C++11 中使用模板元编程连接列表

c++ - 编译时多项式

c++ - 在运行时将 ID 解析为派生类型

d - 是否可以避免代表的 GC?

windows - 为什么 Windows 函数在 D extern 中?

c++ - 在 C++ 模板中使用尖括号会带来哪些语法问题?

c++ - 带有指向成员模板参数的指针的隐含类型?