constraint on variadic template

H. S. Teoh via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Dec 7 09:05:11 PST 2016


On Wed, Dec 07, 2016 at 04:35:52PM +0000, Alex via Digitalmars-d-learn wrote:
> Hi people,
> have searched the history, but didn't find something similar:
> 
> say I have a template like
> 
> mixin template S(T...)
> {
>     void run()
>     {
>         foreach(s; T)
>         {
>             static assert(__traits(hasMember, s, "run"));
>         }
>     }
> }
> 
> How to formulate the check inside the foreach as a template constraint with
> mixin template S(T...) if(???)
[...]

Try this:

	import std.meta : allSatisfy;

	enum hasRun(T) = __traits(hasMember, T, "run");

	mixin template S(T...)
	    if (allSatisfy!(hasRun, T))
	{
	    void run()
	    {
		foreach(s; T)
		{
		    static assert(__traits(hasMember, s, "run"));
		}
	    }
	}


T

-- 
The richest man is not he who has the most, but he who needs the least.


More information about the Digitalmars-d-learn mailing list