是否可以将 `T` 转换为 `ref T`?
Is it possible to transform `T` to `ref T`?
alias refT(T) = ref T;
void test1(refT!int v){
v = 42;
}
void test2(ref int v){
v = 42;
}
void main()
{
import std.stdio;
int i = 5;
test1(i);
writeln(i); // 5
test2(i);
writeln(i); // 42
}
test1
不会突变 i
这告诉我它没有捕获 i
作为参考。 ref
是不是特殊限定符不能这样用?
Although some keywords can be used both as a type qualifier and a storage class, there are some storage classes that cannot be used to construct new types. One example is ref.
alias refT(T) = ref T;
void test1(refT!int v){
v = 42;
}
void test2(ref int v){
v = 42;
}
void main()
{
import std.stdio;
int i = 5;
test1(i);
writeln(i); // 5
test2(i);
writeln(i); // 42
}
test1
不会突变 i
这告诉我它没有捕获 i
作为参考。 ref
是不是特殊限定符不能这样用?
Although some keywords can be used both as a type qualifier and a storage class, there are some storage classes that cannot be used to construct new types. One example is ref.