How define accepted types in a template parameter?

Ferhat Kurtulmuş aferust at gmail.com
Sat Jan 16 20:28:24 UTC 2021


On Saturday, 16 January 2021 at 18:39:03 UTC, Marcone wrote:
> For example, I want my function template to only accept integer 
> or string;

There are different ways of doing that. I'd say this one is easy 
to follow:

import std.stdio;

void print(T)(T entry) if(is(T==string) || is(T==int))
{
	writeln(entry);

}

void main(){
     int i = 5;
     string foo = "foo";

     double j = 0.6;

     print(i);
     print(foo);
     print(j); // compilation error
}


More information about the Digitalmars-d-learn mailing list