Pattern matching within a template constraint

This question is based on Andreiโ€™s answer to my question about signature restrictions.

struct S(int x, int y) { void fun(T)(T t) if (is(TU == S!(a, b), int a, int b)) { } } template s(int a, int b) { enum result = S!(a,b)(); alias result s; } void main() { auto s1 = S!(1, 1)(); auto s2 = S!(2, 2)(); auto s3 = s!(3, 3); auto s4 = s!(4, 4); s1.fun(s1); // ok s1.fun(s2); // ok s1.fun(s3); // compile error s3.fun(s1); // ok s3.fun(s3); // compile error s3.fun(s4); // compile error } 

I do not understand why the code generates compilation errors. Any ideas?

+3
source share
1 answer

First, I would not recommend using a bare template to instantiate an object / structure, because you essentially need the object to be CTFE-capable. If you need an instance, the best option is to return it from the template function:

 @property S!(a, b) s(int a, int b)() { return S!(a, b)(); } 

However, this does not seem to work with the template constraint. I think this should be a foreground mistake. From what I can say, it seems that the return type cannot be correctly checked in the is () expression if it has not already been created somewhere else, for example:

 struct S(int x, int y) { void fun(T)(T t) if (is(TU == S!(a, b), int a, int b)) { } } @property S!(a, b) s(int a, int b)() { return S!(a, b)(); } void main() { auto s1 = S!(1, 1)(); auto s2 = S!(2, 2)(); // comment out and you get errors in fun() call auto s3 = s!(2, 2); s1.fun(s3); } 

I will write this as a mistake.

Edit: Filed as Issue 8493 .

+3
source

All Articles