【发布时间】:2012-04-13 17:50:40
【问题描述】:
struct S(T : T*) {
T t; // t is supposed to be of type 'int*', but it's of type 'int', why?
}
void main() {
int x = 123;
S!(int*) s;
static assert(is(typeof(s.t) == typeof(&x)));
}
以上代码无法编译。
奇怪的是,以下代码确实可以编译:
struct S(T : int*) {
T t;
}
void main() {
int x = 123;
S!(int*) s;
static assert(is(typeof(s.t) == typeof(&x)));
}
我不明白这种行为。将不胜感激。
【问题讨论】:
-
如果你尝试
struct S(T:void*) -
我会指出这些断言不起作用(当然,它们在 2.059 上不起作用),因为您无法比较类型。您需要它们位于
is表达式中。 -
@JonathanMDavis 不管怎样,在第一个示例中执行
s.t = &x不起作用,我得到Error: cannot implicitly convert expression (& x) of type int* to int。所以我猜这是一个 DMD 错误? -
@Arlen 我不是在争论这个问题本身。你的意思很清楚,如果你使用正确的
is表达式,问题确实存在。我只是指出这些断言是不正确的。 -
@JonathanMDavis 谢谢,我修好了。