gpt4 book ai didi

d - 接口(interface)和模板函数

转载 作者:行者123 更新时间:2023-12-04 12:33:01 25 4
gpt4 key购买 nike

我试图在两个不同的类上建立接口(interface),其中函数的实现在子类中。它适用于常规函数,但不幸的是不适用于模板函数。

看例子:

import std.conv;
import std.stdio;

interface Num {
T num(T)();
}

class A : Num {
T num(T)() {
return 5.to!T;
}
}

class B : Num {
T num(T)() {
return 2.to!T;
}
}

void main() {
auto a = new A();
auto b = new B();
Num somea = a;
Num someb = b;
writeln(a.num!int());
writeln(somea.num!int());
writeln(someb.num!int());
writeln(somea.num!string());
writeln(someb.num!string());
}

(也可在线获得:https://run.dlang.io/is/Nl1edV)

结果是错误的:

onlineapp.d:26: error: undefined reference to '_D9onlineapp3Num__T3numTiZQhMFZi'
onlineapp.d:27: error: undefined reference to '_D9onlineapp3Num__T3numTiZQhMFZi'
onlineapp.d:28: error: undefined reference to '_D9onlineapp3Num__T3numTAyaZQjMFZQj'
onlineapp.d:29: error: undefined reference to '_D9onlineapp3Num__T3numTAyaZQjMFZQj'
collect2: error: ld returned 1 exit status
Error: linker exited with status 1

我想要的有可能实现吗?如果是,怎么办?

最佳答案

接口(interface)需要具体类型,以便编译器知道在虚函数表中为每个类保留多少个槽。它还需要足够的信息来可靠地判断接口(interface)是否实际实现。

对于像这样的转换案例,我只是列出了所需的具体类型。 static foreach 可以提供帮助。考虑以下代码:

import std.conv;
import std.stdio;
import std.meta : AliasSeq; // used for the static foreach list

interface Num {
// this is the templated interface. You are allowed to have final
// template members in an interface, with a body included.
public final T num(T)() {
T tmp;
numImpl(tmp); // this forwards to the virtual function...
return tmp;
}

// Here is the explicit list of types we want supported in the interface
// it must be listed so the compiler knows how many vtable slots to assign
protected alias NumImplTypes = AliasSeq!(int, string);
// and now it declares the functions. To follow D overload rules, the
// arguments for each must be different; we can't just rely on return
// types. That's why I did it as a ref thing.
static foreach(T; NumImplTypes)
protected void numImpl(ref T t);
}

class A : Num {
// and now, in each child class, we just do the foreach implementation,
// looking very similar to the template. But it isn't a template anymore
// which allows it to be virtual.
static foreach(T; NumImplTypes)
protected void numImpl(ref T t) {
t = 5.to!T;
}
}

class B : Num {
// ditto
static foreach(T; NumImplTypes)
protected void numImpl(ref T t) {
t = 2.to!T;
}
}

// this is the same as in your example
void main() {
auto a = new A();
auto b = new B();
Num somea = a;
Num someb = b;
writeln(a.num!int());
writeln(somea.num!int());
writeln(someb.num!int());
writeln(somea.num!string());
writeln(someb.num!string());
}

关于d - 接口(interface)和模板函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57599073/

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