template evaluation

Alex sascha.orlov at gmail.com
Sat Apr 14 08:25:58 UTC 2018


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


More information about the Digitalmars-d-learn mailing list