Bug 19710 – template parameters not deduced when one is is a supplied string, and the other inferred.
Status
RESOLVED
Resolution
WONTFIX
Severity
regression
Priority
P3
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2019-02-28T23:51:28Z
Last change time
2019-03-04T00:54:12Z
Assigned to
No Owner
Creator
Alex
Comments
Comment #0 by alex — 2019-02-28T23:51:28Z
import std.stdio;
void attribute(string attributeName,T)(ref T x)
{
mixin(
"x." ~ attributeName ~ " = 10;"
);
}
void main()
{
class A
{
int b;
void load()
{
attribute!"b"(this); //error: template main.attribute cannot deduce function from argument types !("b")(A), candidates are: attribute!"b"(this);
}
}
auto a = new A;
attribute!"b"(a); //OK.
}
This compiles in gdc 6, but fails in gdc8 and dlang.org web compiler.
Comment #1 by sprink.noreply — 2019-03-02T23:04:10Z
Manually insert the type and you will see "this" is considered as an rvalue.
It was deprecated in DMD 2.067.1 to 2.071.2:
Deprecation: this is not an lvalue
Comment #2 by alex — 2019-03-04T00:54:12Z
The solution I found was to make two versions of this attribute function with different names.
One for structs and one for classes.
The ref is necessary to have it work for structs.
The ref fails when used with 'this' in classes.
I think the underlying problem is that in the class version, the ref refers to the object reference (pointer), not the object pointed to, so the ref is unnecessary, and in the case where this is passed, illegal.
In the struct version, the ref refers to the object itself, and is necessary to prevent it being copied and operations happening to a local version.
This is a bit unfortunate, it would be nice to have a template that could work with either.