A devil self reference

Alex sascha.orlov at gmail.com
Wed Aug 15 17:01:23 UTC 2018


On Wednesday, 15 August 2018 at 16:40:49 UTC, Alex wrote:
> Hi all.
> Finally, I arrived at something like this:
>
> ´´´
> void main()
> {
> 	S!(sarr)[] sarr;
> }
>
> struct S(alias Reference)
> {
> 	size_t id()
> 	in
> 	{
> 		// not static assert, only because a pointer is never known 
> in advance
> 		assert(Reference.ptr <= &this);
> 	}
> 	do
> 	{
> 		return &this - Reference.ptr;
> 	}
> }
> ´´´
>
> Of course, this does not compile, because I try to use an 
> identifier before I defined it.
>
> However, the reason I dare to ask is, that all the stuff with 
> the weird self reference is compile time known.
>
> So, is there a possibility to define something like the thing 
> above?

containment in a container is also wanted...

struct Container
{
	S!(sarr)[] sarr;
}

but also not possible in this form because of a circular 
reference.


More information about the Digitalmars-d-learn mailing list