digitalmars.D.learn - Issue with template constraints in numeric types
- data pulverizer (35/35) Aug 03 2017 Dear all,
- data pulverizer (4/6) Aug 03 2017 That is T test(T: double)(T x, T y){...} and T test(T)(T x, T
- Adam D. Ruppe (7/9) Aug 03 2017 Are you familiar with isFloatingPoint?
- data pulverizer (18/28) Aug 03 2017 Thank you very much!
- data pulverizer (4/20) Aug 03 2017 Hmm ... it looks as the specialization `:` operator is working
- jmh530 (2/5) Aug 04 2017 They're both doing the same thing as far as I know.
Dear all,
I am writing template constraints for different numeric types:
```
import std.stdio: writeln;
import std.traits: isIntegral, isNumeric;
T test(T)(T x, T y)
if(is(T: double) && isNumeric!T)
{
return x*y;
}
auto test(T)(T x, T y)
if(!is(T: double) && isNumeric!T)
{
return 5*test!double(x, y);
}
void main()
{
int x = 2;
double y = 2.0;
writeln("int test: ", test(x, x));
writeln("double test: ", test(y, y));
}
```
returns:
```
int test: 4
double test: 4
```
The same issue occurs when I try using template specializations
instead. Explanations and suggestions please.
Thank you!
Compiler details:
$ dmd --version
DMD64 D Compiler v2.074.1
Copyright (c) 1999-2017 by Digital Mars written by Walter Bright
Aug 03 2017
On Thursday, 3 August 2017 at 12:24:02 UTC, data pulverizer wrote:The same issue occurs when I try using template specializations instead. ...That is T test(T: double)(T x, T y){...} and T test(T)(T x, T y){...} Thanks
Aug 03 2017
On Thursday, 3 August 2017 at 12:24:02 UTC, data pulverizer wrote:import std.traits: isIntegral, isNumeric;Are you familiar with isFloatingPoint? http://dpldocs.info/experimental-docs/std.traits.isFloatingPoint.htmlif(is(T: double) && isNumeric!T)Keep in mind that T:double here means "if T can implicitly convert to double". Since int can implicitly convert to double too, this case covers both families! You might want to try == instead of : for a more exact match.
Aug 03 2017
On Thursday, 3 August 2017 at 12:31:00 UTC, Adam D. Ruppe wrote:On Thursday, 3 August 2017 at 12:24:02 UTC, data pulverizer wrote:Thank you very much! What about this case: ``` T test(T: double)(T x, T y) { return x*y; } auto test(T)(T x, T y) { return 5*test!double(x, y); } ``` which also gives: ``` int test: 4 double test: 4 ```import std.traits: isIntegral, isNumeric;Are you familiar with isFloatingPoint? http://dpldocs.info/experimental-docs/std.traits.isFloatingPoint.htmlif(is(T: double) && isNumeric!T)Keep in mind that T:double here means "if T can implicitly convert to double". Since int can implicitly convert to double too, this case covers both families! You might want to try == instead of : for a more exact match.
Aug 03 2017
On Thursday, 3 August 2017 at 12:35:08 UTC, data pulverizer wrote:
What about this case:
```
T test(T: double)(T x, T y)
{
return x*y;
}
auto test(T)(T x, T y)
{
return 5*test!double(x, y);
}
```
which also gives:
```
int test: 4
double test: 4
```
Hmm ... it looks as the specialization `:` operator is working
like the constraint `:` operator and doing convertible at least
for the floating point case. Is that right?
Aug 03 2017
On Thursday, 3 August 2017 at 12:49:48 UTC, data pulverizer wrote:Hmm ... it looks as the specialization `:` operator is working like the constraint `:` operator and doing convertible at least for the floating point case. Is that right?They're both doing the same thing as far as I know.
Aug 04 2017









data pulverizer <data.pulverizer gmail.com> 