Template alias parameter: error: need 'this' for ...

Elfstone elfstone at yeah.net
Fri Feb 24 12:00:41 UTC 2023


https://forum.dlang.org/post/imnannjdgtjnlzevhwez@forum.dlang.org

On Saturday, 24 August 2013 at 11:47:43 UTC, Matej Nanut wrote:
> On Friday, 23 August 2013 at 22:54:33 UTC, Jonathan M Davis 
> wrote:
>> Because without static it's a member variable, which means 
>> that you have to
>> have a constructed object to access it (since it's part of the 
>> object). When
>> you declare a variable in a class or struct static, then 
>> there's only one for
>> the entire class or struct, so it can be accessed without an 
>> object. And when
>> you do StructName.var or ClassName.var your accessing the 
>> variable via the
>> struct or class rather than an object, so the variable must be 
>> static.
>>
>> - Jonathan M Davis
>
> But I declared the template static, not the variable.
>
> Is there a better way to pass a ‘member get’ expression to a 
> template?
>
> I need this for calling ‘.offsetof’ on it, and for checking if 
> the member's parent is a certain struct type.

Seems like the same bug is still there after ten years.


	struct Bar
	{
		@("hello") int t;
	}

	static bool hasAttribute(alias F, T)()
	{
		bool result = false;
		foreach (a; __traits(getAttributes, F))
		{
			static if (is(typeof(a) : T))
			{
				result = true; // couldn't simply return true, 'cause the 
compiler complains about "unreachable code".
			}
		}
		return result;
	}

	void main()
	{
		import std.stdio;

		writeln(hasAttribute!(Bar.t, string));
	}


More information about the Digitalmars-d-learn mailing list