Erroneous "auto can only be used for template function parameters"?
Yuxuan Shui via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Mon Jun 22 16:02:11 PDT 2015
On Saturday, 20 June 2015 at 01:50:11 UTC, Yuxuan Shui wrote:
> Try to compile this code snippet:
>
> import std.traits;
> template a(R) {
> auto a(S)(auto ref R i) {
> return cast(S)i*2;
> }
> }
> template ReturnTypeEx(alias A, B) {
> alias ReturnTypeEx = ReturnType!(A!B);
> }
> template b(alias R) {
> int b(S)(S i) {
> alias Ra = ReturnTypeEx!(R, S);
> return cast(int)R!S(i);
> }
> }
> void main() {
> alias bb = b!(a!ulong);
> pragma(msg, ReturnTypeEx!(bb, int));
> }
>
> DMD reports:
> sadf.d(3): Error: auto can only be used for template function
> parameters
> sadf.d(8): Error: template instance sadf.a!ulong.A!int error
> instantiating
> sadf.d(12): instantiated from here: ReturnTypeEx!(a, int)
> sadf.d(8): instantiated from here: A!int
> sadf.d(18): instantiated from here: ReturnTypeEx!(b, int)
> sadf.d(18): while evaluating pragma(msg,
> ReturnTypeEx!(b, int))
>
> But a(S)(auto ref R) is indeed a template function, so I don't
> understand.
After more experimenting, here's what I got. This time no nested
templates are involved, this seems more likely an 'auto ref' bug.
import std.traits, std.range;
void a(S)(auto ref S i) { }
void b(S)(auto ref S i) if (isInputRange!S) { }
void c(S)(ref S i) if (isInputRange!S) { }
void devil(alias S)() { }
void main() {
a!string(""); //Works <--- This line affects the result of
devil!(a!string)
b!string(""); //Works
//Next line is weird, it:
//1. Err, 'auto ref can only be used with template function',
if 'a' is not
// instantiated with 'a!string' first
//2. Works, if 'a!string' is done first
alias x = devil!(a!string);
alias xx = devil!(b!string); //Err, template doesn't match
alias xxx = devil!(c!string); //Works
}
I'm using DMD 2.067.1
More information about the Digitalmars-d-learn
mailing list