www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Create constraint for each parameter in template arg pack

reply Andrey <saasecondbox yandex.ru> writes:
Hello,
Let we have two variadic templates:
 template Qwerty(Values...) {}
 template Qaz(alias type, Data...) {}
Now I want to add a constraint to "Qwerty" so that each type in "Values" pack must be a "Qaz" template. I don't care about values of "type" or "Data" in "Qaz". How to do it in D?
Aug 28 2018
next sibling parent vit <vit vit.vit> writes:
On Tuesday, 28 August 2018 at 12:28:19 UTC, Andrey wrote:
 Hello,
 Let we have two variadic templates:
 template Qwerty(Values...) {}
 template Qaz(alias type, Data...) {}
Now I want to add a constraint to "Qwerty" so that each type in "Values" pack must be a "Qaz" template. I don't care about values of "type" or "Data" in "Qaz". How to do it in D?
import std.meta : allSatisfy; template Qwerty(Values...) if(allSatisfy!(isQaz, Values)){ } template Qaz(alias type, Data...) {} enum isQaz(alias Q : Qaz!Args, Args...) = true; enum isQaz(Args...) = false; alias Foo = Qwerty!(Qaz!(i => i), Qaz!(i => i), Qaz!(i => i));
Aug 28 2018
prev sibling next sibling parent reply bauss <jj_1337 live.dk> writes:
On Tuesday, 28 August 2018 at 12:28:19 UTC, Andrey wrote:
 Hello,
 Let we have two variadic templates:
 template Qwerty(Values...) {}
 template Qaz(alias type, Data...) {}
Now I want to add a constraint to "Qwerty" so that each type in "Values" pack must be a "Qaz" template. I don't care about values of "type" or "Data" in "Qaz". How to do it in D?
I'm not sure if there is a better way, but isInstanceOf (std.traits) seems to work with a static foreach and a static if. template Qwerty(Values...) { static foreach (value; Values) { static if (!isInstanceOf!(Qaz, value)) { static assert(0, "Values are not Qaz only ..."); } } // ... }
Aug 28 2018
next sibling parent Alex <sascha.orlov gmail.com> writes:
On Tuesday, 28 August 2018 at 13:05:15 UTC, bauss wrote:
 On Tuesday, 28 August 2018 at 12:28:19 UTC, Andrey wrote:
 Hello,
 Let we have two variadic templates:
 template Qwerty(Values...) {}
 template Qaz(alias type, Data...) {}
Now I want to add a constraint to "Qwerty" so that each type in "Values" pack must be a "Qaz" template. I don't care about values of "type" or "Data" in "Qaz". How to do it in D?
I'm not sure if there is a better way, but isInstanceOf (std.traits) seems to work with a static foreach and a static if. template Qwerty(Values...) { static foreach (value; Values) { static if (!isInstanceOf!(Qaz, value)) { static assert(0, "Values are not Qaz only ..."); } } // ... }
https://dlang.org/phobos/std_traits.html#TemplateOf maybe...
Aug 28 2018
prev sibling parent Andrey <saasecondbox yandex.ru> writes:
On Tuesday, 28 August 2018 at 13:05:15 UTC, bauss wrote:
 I'm not sure if there is a better way, but isInstanceOf 
 (std.traits) seems to work with a static foreach and a static 
 if.

 template Qwerty(Values...)
 {
     static foreach (value; Values)
     {
         static if (!isInstanceOf!(Qaz, value))
         {
             static assert(0, "Values are not Qaz only ...");
         }
     }

     // ...
 }
Thank you everybody!
Aug 28 2018
prev sibling parent vit <vit vit.vit> writes:
On Tuesday, 28 August 2018 at 12:28:19 UTC, Andrey wrote:
 Hello,
 Let we have two variadic templates:
 template Qwerty(Values...) {}
 template Qaz(alias type, Data...) {}
Now I want to add a constraint to "Qwerty" so that each type in "Values" pack must be a "Qaz" template. I don't care about values of "type" or "Data" in "Qaz". How to do it in D?
Generic solution: template Qwerty(Values...) if(allSatisfy!(isInstanceOf!Qaz, Values)){ } class Qaz(alias type, Data...) {} import std.meta : allSatisfy; public import std.traits : isInstanceOf; template isInstanceOf(alias T){ import std.traits : impl = isInstanceOf; static enum isInstanceOf(alias X) = impl!(T, X); static enum isInstanceOf(X) = impl!(T, X); }
Aug 28 2018