gpt4 book ai didi

templates - 内省(introspection)模板参数(尤其是模板别名参数)

转载 作者:行者123 更新时间:2023-12-02 04:05:19 26 4
gpt4 key购买 nike

假设我有一个 D 模板结构:

struct Foo(alias pred) {}

我想写一个模板来反省 pred 是什么:
alias Foo!"a < b" FooLess;
static assert(introspectPred!(FooLess) == "a < b");

有什么简单的方法可以做到这一点?如果是这样,如何 introspectPred被执行?

最佳答案

我唯一能想到的就是在 Foo 中定义一个枚举。输入并做一些基本的反省。例如。:

struct Foo(alias pred) 
{
enum string _pred = pred;
}

alias Foo!"a < b" FooLess;
static assert(introspectPred!(FooLess) == "a < b");

template introspectPred(T)
{
enum string introspectPred = getMember!(T, "_pred");
}

template getMember(T, string member)
{
static if (__traits(hasMember, T, member))
{
enum getMember = mixin("T." ~ member);
}
else
enum getMember = "";
}

void main()
{

}

关于templates - 内省(introspection)模板参数(尤其是模板别名参数),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8017966/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com