두 가지 불변 범위의 직교 곱을 계산할 수없는 이유는 무엇입니까?불변 범위의 데카르트 곱
다음 코드
import std.stdio;
import std.algorithm;
void main() {
immutable int[] B = [ 1, 2, 3 ];
immutable int[] C = [ 4, 5, 6 ];
auto BC = cartesianProduct(B, C);
writeln(BC);
}
예외 : 제 하지만 불변 제가 제거되면
/usr/include/dmd/phobos/std/range.d(4199): Error: cannot modify struct result._ranges_field_1 Repeat!(immutable(int)) with immutable members
/usr/include/dmd/phobos/std/range.d(4503): Error: template instance std.range.Zip!(immutable(int)[], Repeat!(immutable(int))) error instantiating
/usr/include/dmd/phobos/std/algorithm.d(11674): instantiated from here: zip!(immutable(int)[], Repeat!(immutable(int)))
laurent_test.d(8): instantiated from here: cartesianProduct!(immutable(int)[], immutable(int)[])
/usr/include/dmd/phobos/std/algorithm.d(11674): Error: template instance std.range.zip!(immutable(int)[], Repeat!(immutable(int))) error instantiating
laurent_test.d(8): instantiated from here: cartesianProduct!(immutable(int)[], immutable(int)[])
laurent_test.d(8): Error: template instance std.algorithm.cartesianProduct!(immutable(int)[], immutable(int)[]) error instantiating
Futhermore, 그것은 작동한다.
phobos 구현에 따르면, inputRange와 forwardRange가되는 범위 중 하나입니다. 그러한 템플릿 제약이 왜 필요한가?
사람들이 종종 의도하는 것은 변경 불가능한 멤버의 변경 가능한 배열입니다. '불변 (int) []'. 'immutable int []'는'immutable (int [])'과 동일합니다. 이것은 첫 번째 타입보다 더 제한적입니다. 특히 문제가 해결되지 않아서 문제에 적용된다고 말할 수 없습니다. –