3
struct A{
int[3] _data;
ref int opIndex(size_t i) { return _data[i]; }
int opIndex(size_t i) const{ return _data[i]; }
}
T fun(T)(const ref T a){
T ai = a;
swap(ai[0], ai[1]); // error
return ai;
}
immutable A a = A();
immutable A b = fun(a);
void main(){ }
上面的代碼提供了以下錯誤:opIndex的正確實現是什麼?
Error: ai.opIndex(0LU) is not an lvalue
Error: ai.opIndex(1LU) is not an lvalue
called from here: fun(a)
ai
是a
副本,它是一個左值,所以我不明白爲什麼我得到的錯誤。
在哪裏分配? – Arlen 2012-01-31 18:58:09
哦,我明白了,忘了交換。看起來好像你沒有得到實際的錯誤,嘗試取消註釋交換,你會得到一個新的錯誤:'錯誤:靜態變量a不能在編譯時引用',這似乎是實際的問題。此外'T ai = a;'給你一個不可變的結構。 – dav1d 2012-01-31 19:14:35
好的。那麼'T ai = a;'不應該給一個不變的。反正有沒有這樣的表現? – Arlen 2012-01-31 19:32:01