【发布时间】:2011-08-07 21:49:25
【问题描述】:
我正在尝试在 D 中实现我自己的范围,但我在使用它的 .front() 方法时遇到了问题。
编辑:
我需要返回值是ref。
-
如果我把它设为
const,那么返回的对象将是一个副本,这不是我想要的。 -
如果我不将其设为
const,那么我根本无法在我的范围的const副本上使用.front。
我该如何解决这个问题?
struct MyRange(T)
{
T[] buf;
@property ref T front() { return this.buf[0]; } // No error, but not const
@property ref T front() const { return this.buf[0]; } // Errors
@property T front() const { return this.buf[0]; } // No error, but a copy
// Can't have all three
}
【问题讨论】: