Bug 10262 – utf.decodeFront doesn't work with a string slice
Status
RESOLVED
Resolution
INVALID
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-06-04T00:34:00Z
Last change time
2013-06-04T00:59:47Z
Assigned to
nobody
Creator
gdkslpmq
Comments
Comment #0 by gdkslpmq — 2013-06-04T00:34:58Z
The code
import std.utf;
void main() {
string str = "asdf";
size_t size;
decodeFront(str[1..$], size);
}
fails to compile, with the error
test.d(8): Error: template std.utf.decodeFront does not match any function template declaration. Candidates are:
/usr/include/d/std/utf.d(978): std.utf.decodeFront(S)(ref S str, out size_t numCodeUnits) if (!isSomeString!(S) && isInputRange!(S) && isSomeChar!(ElementType!(S)))
/usr/include/d/std/utf.d(1012): std.utf.decodeFront(S)(ref S str, out size_t numCodeUnits) if (isSomeString!(S))
/usr/include/d/std/utf.d(1038): std.utf.decodeFront(S)(ref S str) if (isInputRange!(S) && isSomeChar!(ElementType!(S)))
/usr/include/d/std/utf.d(978): Error: template std.utf.decodeFront cannot deduce template function from argument types !()(string, ulong)
Casting str[1..$] to a string explicitly results in the same error. Putting the slice outside the function call, like
string s2 = str[1..$];
decodeFront(s2, size);
Fixed the error.
Comment #1 by issues.dlang — 2013-06-04T00:50:53Z
It's not supposed to work with a slice like that. It accepts arguments by ref and pops off elements from the range that it's given, so it requires that you give it an lvalue, which is what you did with
string s2 = str[1..$];
decodeFront(s2, size);
It accepted rvalues in 2.062, but that resulted in very inconsistent and potentially buggy behavior (depending on the type of range), so it now accepts the range by ref.