나는 클래스 기반의 유한 랜덤 액세스 범위에서 작업 해왔다. 4838 | | 오류 "algorithm.d : foreach는 : 전자 심판을 할 수 없습니다 그것에 몇 가지 검사를 수행 할 때 :std.algorithm.find가 범위 요소에 대한 참조를 요구해야합니까?
auto myRange = /* construct my range */
static assert (isRandomAccessRange!(typeof(myRange))); //
static assert (!isInfinite!(typeof(myRange))); // both pass
auto preamble = myRange[0..128];
assert(all!"a == 0"(preamble)); // check for all zeros
나는 위의 코드 조각의 마지막 줄에 관한, GDC 4.9.2이 컴파일 오류가 발생했습니다 "
실제로 foreach
각 요소에 대한 참조 소요 std.algorithm.find
에서이 코드 조각합니다 (find_if 변형, 범위와 조건을 고려)에 오류 점 :
InputRange find(alias pred, InputRange)(InputRange haystack)
if (isInputRange!InputRange)
{
alias R = InputRange;
alias predFun = unaryFun!pred;
static if (isNarrowString!R)
{
...
}
else static if (!isInfinite!R && hasSlicing!R && is(typeof(haystack[cast(size_t)0 .. $])))
{
size_t i = 0;
foreach (ref e; haystack) // <-- needs a ref
{
if (predFun(e))
return haystack[i .. $];
++i;
}
return haystack[$ .. $];
}
else
{
...
}
}
이 가능성이 가장 높은 발생을 내가 제공했기 때문에 ref
인수를 제공하지 않는 opApply
구현 (클래스는 다른 멤버 함수에 ref
반환 유형을 제공하지 않음).
int opApply(int delegate(E) f) {...}
int opApply(int delegate(size_t,E) f) {...}
는 그 변경,하지만 정말 날 귀찮게하는 것은 지금 범위 클래스는 함수의 전제 조건에 부합하며, foreach
반복이 여전히 어쨌든 그들과 함께 작업해야한다는 것입니다 수 있습니다. 문서에서 인용 :
이들 모두가 제공되었지만 (그렇지 않은 경우 무작위 액세스 범위가 아닐 수 있습니다.) 따라서이를 사용해야합니다. 대신, 다음에 설명하는 대체 반복 방법을 찾고있을 수 있습니다.
If the aggregate expression is a struct or class object, and the range properties do not exist, then the foreach is defined by the special
opApply
member function and the foreach_reverse behavior is defined by the specialopApplyReverse
member function. These functions have the type:
int opApply(int delegate(ref Type [, ...]) dg);
내 해석에 따르면 안되는 것은 아닙니다.
또한 std.algorithm.all
을 인용 한 참고 문헌에 대한 반복을 요구하지 않는 것 중 하나
bool all(Range)(Range range) if (isInputRange!Range && is(typeof(unaryFun!pred(range.front))));
Returns true if and only if all values v found in the input range range satisfy the predicate pred. Performs (at most) Ο(range.length) evaluations of pred.
그래서이 포보스 라이브러리의 버그이며, std.algorithm.find
는 처음부터 값으로 반복해야
실제로, 그것은 그것을 못 박았습니다. D 커뮤니티는 이와 같은 결함을 찾기 위해 좀 더 우둔한 프로그래머를 사용할 수도 있습니다. ;) –