阅读模板 - 重访:
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)));
}
Run Code Online (Sandbox Code Playgroud)
上面的代码没有编译.
奇怪的是,以下编译:
struct S(T : int*) {
T t;
}
void main() {
int x = 123;
S!(int*) s;
static assert(is(typeof(s.t) == typeof(&x)));
}
Run Code Online (Sandbox Code Playgroud)
我不明白这种行为.一个解释将不胜感激.
当类型特化(冒号后的类型)依赖于参数标识符时,例如T : T*,如果存在匹配,则结果标识符引用标识符(T)在类型特化(推导类型)中的角色.
否则,如果特化是独立的,例如T : int*,结果标识符是特化类型的别名.
例子:
=========================================================
Argument T | Specialization | Result
=========================================================
void | T : void | void
char | T : void | <no match>
int* | T : T* | int
immutable(char)[] | T : T[] | immutable(char)
immutable(char)[] | T : immutable(T)[] | char
=========================================================
Run Code Online (Sandbox Code Playgroud)
传递给模板参数的参数不匹配时,模板将从重载集中删除.如果在找到匹配项之前过载集变空,则会引发错误.
当IsExpression(is(...)主表达式)不匹配时,结果为false,并且没有符号引入范围.