gpt4 book ai didi

templates - D中的递归模板问题

转载 作者:行者123 更新时间:2023-12-02 10:44:44 24 4
gpt4 key购买 nike

我第一次尝试在d中使用递归模板,但我迷路了:

import std.stdio, std.conv, orange.util.Reflection;

struct Foo {
int a = 7;
int b = 4;
int c = 5;
}

void main(string[] args) {
pragma(msg,dictString!Foo);
}

template ObjDict(T) {

}

template dictString (T) {
const dictString = dictStringImpl!(T, 0);
}

template dictStringImpl (T, size_t i) {
static if(T.tupleof.length == 0)
const dictString = "";
else static if(T.tupleof.length -1 == i) {
pragma(msg, to!string("i = "~to!string(i)~" name = "~nameOfFieldAt!(T,i)));
const dictString = "dict[\"" ~ nameOfFieldAt!(T,i) ~ "\"] = " ~ nameOfFieldAt!(T,i) ~ ";";
pragma(msg,"last one "~to!string(i)~" \""~dictString~"\"");
}
else {
pragma(msg, to!string("i = "~to!string(i)~" name = "~nameOfFieldAt!(T,i)));
const dictString = "dict[\"" ~ nameOfFieldAt!(T,i) ~ "\"] = " ~ nameOfFieldAt!(T,i) ~ ";\n" ~ dictStringImpl!(T, i+1);
}
}

编译后可得出:
i = 0 name = a
i = 1 name = b
i = 2 name = c
last one 2 "dict["c"] = c;"
orange_test.d(31): Error: void has no value
orange_test.d(31): Error: incompatible types for (("dict[\"" ~ nameOfFieldAt ~ "\"] = " ~ nameOfFieldAt ~ ";\x0a") ~ (dictStringImpl!(Foo,2LU))): 'string' and 'void'
orange_test.d(31): Error: template instance orange_test.dictStringImpl!(Foo,1LU) error instantiating
orange_test.d(18): instantiated from here: dictStringImpl!(Foo,0)
orange_test.d(10): instantiated from here: dictString!(Foo)
orange_test.d(18): Error: template instance orange_test.dictStringImpl!(Foo,0) error instantiating
orange_test.d(10): instantiated from here: dictString!(Foo)
orange_test.d(10): Error: template instance orange_test.dictString!(Foo) error instantiating
orange_test.d(10): while evaluating pragma(msg, dictString!(Foo))

如您所见,橙色的nameOfFieldAt模板没有问题,但是我认为递归出了点问题。

任何帮助将不胜感激,我很想让这个工作。

请参阅此处以获取具有相关橙色模板(以及其他一些模板)的版本 http://dpaste.dzfl.pl/4b7a36f8

最佳答案

在dictStringImpl模板中,您仍使用dictString作为同名:)
并没有进一步寻找,但这将修复与空相关的错误。

关于templates - D中的递归模板问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12842554/

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