how to do template constraints with variadic templates ?

Flaze07 christianseiji.cs at gmail.com
Sun Jul 8 00:52:41 UTC 2018


On Sunday, 8 July 2018 at 00:46:13 UTC, Flaze07 wrote:
> I want to force the variadic templates's type to be of certain 
> types, I thought doing this would work
> auto sum( A... )( A a ) if( isIntegral!( typeid( a[ 0 ] ) ) )
> {
>     int temp;
>     foreach( t ; a )
>     {
>         temp += t;
>     }
>     return temp;
> }
> but it gives out error
> ( note : I want all the parameter to be integral, i.e sum( 1, 
> 2, 5, "error" ); //error

I uhh managed to do it with this code
auto sum( A... )( A a )
{
     static foreach( t ; a )
     {
         static assert( isIntegral!( typeof( t ) ) );
     }
     int temp;
     foreach( t ; a )
     {
         temp += t;
     }
     return temp;
}
but, I just want to ask something, is the static assert still 
runned during runtime ?


More information about the Digitalmars-d-learn mailing list