Function literals can't be class members

John Colvin john.loughran.colvin at gmail.com
Tue Jul 9 03:25:25 PDT 2013


On Monday, 8 July 2013 at 22:41:41 UTC, JS wrote:
> trying to use a lambda inside a sub template gives an error.
>
> mixin template a
> {
>    template b()
>    {
>       enum b = { }();
>    }
>    mixin(b!());
> }
>
> gives the error in the subject, removing the nesting or using a 
> function instead of a lamda produces no error, yet all are 
> essentially semantically equivalent... just the lamda version 
> requires less typing. (in fact, it would be nice to have a 
> simplification of template b() = { ... }();)

This belongs in D.learn.

The code you provided is never going to give any "Function 
literals can't be class members" error, although it does have a 
lot of other things wrong with it.

1) The mixin template has no parameter list.
2) enum b = { }(); isn't valid without a function body. Did you 
mean (){} ?
3) and it definitely isn't going to return a string to work with 
mixin();

It helps to give code examples that are at least syntactically 
correct, if not immediately compilable. A correct and simplified 
test case of what I *think* you are talking about is this:

mixin template A()
{
	auto foo = (){ };
}

class C
{
	mixin A!();
}

Error: delegate f977.C.A!().__lambda1 function literals cannot be 
class members

Lambda function and delegate literals cannot be class members. 
Function literals can. I'm not entirely sure on the reason why, 
it's probably to do with context pointers.


More information about the Digitalmars-d mailing list