is this even possible? newbie + mixin template + foreach (allMembers)
Alex
sascha.orlov at gmail.com
Tue Apr 3 19:25:28 UTC 2018
On Tuesday, 3 April 2018 at 18:57:29 UTC, WebFreak001 wrote:
>
> you need to use a static foreach for this. You can insert a
> static foreach basically where you can insert a function
> definition like void foo() {}
>
> foreach is more like a function call like foo(), so you can't
> put it in the mixin template.
>
> A mixin template is basically expected to be mixin-able in
> global scope or in a class/struct so it can't have any things
> like function calls because that would be the same as writing
> `foo();` at global level instead of in the main function.
>
> static foreach on the other hand allows exactly that because it
> literally unrolls the contents and inserts it multiple times,
> so you can also put it in mixin templates if the content of the
> loop is valid in mixin templates.
>
> If you need to support older compilers you can use normal
> foreach with ctfe by generating a code string or do recursive
> templates, but I would really encourage you to use static
> foreach instead, it is cleaner and faster.
I think, the OP has something other in mind. Look at this:
´´´
void main(){}
struct World
{
float rotation;
bool active;
//mixin BuildStuff!();
}
mixin BuildStuff!();
mixin template BuildStuff()
{
static foreach(elem; __traits(allMembers, World))
{
pragma(msg, elem);
}
}
´´´
While the outer mixin works as expected, the inner one results in
a seg fault.
No idea why, however, too...
More information about the Digitalmars-d-learn
mailing list