www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - template evaluation

reply Alex <sascha.orlov gmail.com> writes:
Hi all,
I must overlook something, but
given this:

´´´
void main(){}

static assert(isMatching!(D, S!(D, true))); // place 1: works as 
expected.

struct D
{
	auto static s = S!(typeof(this), true).init;
}

enum bool isMatching(T, U) =
	(){
		bool b;
	        static foreach(i, m; __traits(allMembers, T))
		{
     		        static if(is(typeof(__traits(getMember, T, m)) == 
U))
     		        {
     			       if(b) return false;
     			       else b = true;
     		        }
		}
		return b;
	}();

struct S(T, bool c = false)
	//if(isMatching!(T, S!(T, true))) // place 2: does not work
{}
´´´

While "place 1" works as expected: it static asserts to true, if 
type D has a single coinciding member to the checked one and 
static asserts to false if it doesn't or if there are more of 
them then one;
Why I can't use the same template in "place 2"?


PS: Taken the (){}() pattern from here:
https://p0nce.github.io/d-idioms/#Precomputed-tables-at-compile-time-through-CTFE
Apr 14 2018
parent Alex <sascha.orlov gmail.com> writes:
Ok, trying to reduce my example a little bit, I arrived at this:

´´´
void main(){}

struct D
{
	size_t dummy;
	auto static s = S!D.init;
}

struct S(alias container = null)
{
	pragma(msg, container);
	
	static if(__traits(compiles, __traits(allMembers, container)))
	{
		static foreach(m; __traits(allMembers, container))
		{
			pragma(msg, m);
			
			pragma(msg, is(typeof(__traits(getMember, container, m))));
                         /*
                         for D.dummy this yields "true"
                         for D.s this yields "false"
                         */
		}
	}
}
´´´

What I clearly have, is a circular reference, now I'm aware of 
this.

So, am I trying something illegal? It seems, I can't get the type 
of the according member of the template parameter, even then (or 
should I say "especially because") the analyzing type has the 
same type.
But if this would be the only member, which I can't get the type 
of, the information would be enough, to ensure what I want. Is it 
like this?
Apr 14 2018