Test case:
------------------------
class TestClass
{
int a;
this()
{
enum c = getAttribute!(TestClass.a);
}
}
int getAttribute(alias target)()
{
return 0;
}
------------------------
produces this error in DMD 2.063:
Error: value of 'this' is not known at compile time
This is annoying as you can use e.g. __traits(getAttributes, TestClass.a) but you can not wrap this call into a template because of this bug.
A workaround as mentioned by Andrej Mitrovic is adding static to the getAttribute template declaration.
Comment #1 by clugdbug — 2013-07-25T01:39:22Z
This isn't actually a CTFE issue. Something weird is happening here, maybe related to UFCS.
The declaration of c is actually being transformed into:
enum c = this.getAttribute!(TestClass.a);
and that's the 'this' which CTFE is complaining about. Bizarrely, marking the template as 'static' prevents that incorrect 'this' from being added.
Comment #2 by mk — 2016-02-15T00:03:22Z
Same issue ? static on templated function helps too.
------------------------------------------
int func(T...)()
{
return 1;
}
class Bug
{
Stru s;
this() { }
int pokus()
{
// Error: this for func needs to be type Stru not type strubug.Bug
return func!(s.a)();
}
}
void bug()
{
Stru s;
int pokus()
{
// Error: need 'this' for 'func' of type 'pure nothrow @nogc @safe int()'
return func!(s.a)();
}
}
struct Stru
{
int a;
int b;
}
------------------------------------------------
First example compiles with 2.062
Comment #3 by robert.schadek — 2024-12-13T18:09:46Z