D 隐式函数实例化期间数组常量丢弃

D 隐式函数实例化期间数组常量丢弃,d,D,D在隐式函数实例化期间丢弃顶级数组的常量,在显式函数实例化时保留它。 考虑代码: // main.d import std.stdio; void foo( T )( T val ) { writeln( typeid( T ) ); } void main() { const int[] arr; writeln( typeid( arr ) ); // actual type foo( arr ); // implicit instantiation foo!( type

D在隐式函数实例化期间丢弃顶级数组的常量,在显式函数实例化时保留它。 考虑代码:
// main.d
import std.stdio;
void foo( T )( T val )
{
  writeln( typeid( T ) );
}
void main()
{
const int[] arr; writeln( typeid( arr ) ); // actual type foo( arr ); // implicit instantiation foo!( typeof( arr ) )( arr ); // explicit instantiation }
//main.d
进口std.stdio;
无效foo(T)(T val)
{
writeln(typeid(T));
}
void main()
{
常量int[]arr; writeln(typeid(arr));//实际类型 foo(arr);//隐式实例化 foo!(typeof(arr))(arr);//显式实例化 }
…和输出:
$dmd main.d&&./main
$ dmd main.d && ./main
const(const(int)[])
const(int)[]
const(const(int)[])
常数(常数(整数)[] 常数(整数)[] 常数(常数(整数)[]
如您所见,在隐式实例化的情况下,顶级常量丢失了。这是错误、功能还是我的误解?

丢失的是数组指针的常量,而不是数组本身的常量

D中的
const int[]
保护数组指针(不能将其指向其他数组)和数组数据(不能更改元素)。这就是为什么第一个和第三个输出中有2个
const
s。但是,将数组传递给函数时,不需要保持指针的常量-如果将
val
inside
foo
更改为其他数组,则不会影响
main
函数中
arr
的内容