小编Dav*_*nen的帖子

D:为什么opIndex在std.container.Array类中不是const限定的?

我最近想使用std.container.Array并继续创建一个带有getter成员函数的类,该函数从Array类返回一个值.我很快意识到我无法对我的getter进行const限定,因为opIndex是一个可变函数.

我尝试将源代码更改为const-qualify Array.opIndex,并且它构建得很好.但是,std.algorithm中的某些单元测试没有通过,抱怨Array.opIndex的返回值不是左值.

这是Array.opIndex的代码:

ref T opIndex(size_t i)
{
    version (assert) if (!_data.refCountedStore.isInitialized) throw new RangeError();
    return _data._payload[i];
}
Run Code Online (Sandbox Code Playgroud)

这里有什么我想念的吗?为什么它不符合常量?

d const phobos

2
推荐指数
1
解决办法
115
查看次数

标签 统计

const ×1

d ×1

phobos ×1