【发布时间】:2016-03-25 14:55:39
【问题描述】:
我有一个结构 Foo!T 和一个对任意两个 Foo!T 进行操作的函数。
我希望声明这样一个函数
void fun(U)(U a, U b) if (is(U : Foo!T, T...)) { }
但是,事实证明我可以将其声明为
void fun(U)(U a, U b) if (is(U : Foo)) { }
只有在我声明它在Foo的正文中。
例如:
struct Foo(T) {
void fun1(U)(U b) if (is(U : Foo)) { }
}
void fun2(U)(U a, U b) if (is(U : Foo)) { }
unittest {
Foo!int f;
f.fun1(f);
f.fun2(f);
}
以上调用失败,struct d.Foo(T) is used as a type
fun2。不过fun1 没问题。
为什么约束 is(U : Foo) 在 Foo 的主体内有效,但不是
外部?
Foo 正文内的比较 is(U : Foo) 是否等同于 Foo 正文外的 is(U :
Foo!V, V...)?
【问题讨论】:
-
这有点像虫子的味道,但它也可能是一个奇怪的同名事物...... Foo!T 内部的 Foo 正在扩展为 Foo!T(尝试在内部使用
pragma(msg, Foo)。在外部, 没有实例化。所以它们不一样:在 Foo!int 内部,它会接受其他 Foo!int,但你的 fun2 也会接受 Foo!string 和其他。
标签: templates metaprogramming d