www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Templates and function parametes

reply "Paul D Anderson" <claude.rains msn.com> writes:
When I try to compile these two functions, the second function is 
flagged with an already defined error:

bool testRoundTrip(T, U)(T first, U second) if (isIntegral!T && 
isFloatingPoint!U)
{
     return false;
}
bool testRoundTrip(U, T)(U first, T second) if (isIntegral!T && 
isFloatingPoint!U)
{
     return false;
}

They look to me to be distinct functions, one with an integral 
parameter followed by a floating point parameter and the other 
with a floating point parameter followed by an integral parameter.

At first I thought this was a template problem, but the following 
code also raises the same error.

bool testRoundTrip(int first, double second)
{
     return false;
}
bool testRoundTrip(double first, int second)
{
     return false;
}

What's the problem?
Aug 08 2014
parent reply Rikki Cattermole <alphaglosined gmail.com> writes:
On 9/08/2014 6:19 p.m., Paul D Anderson wrote:
 When I try to compile these two functions, the second function is
 flagged with an already defined error:

 bool testRoundTrip(T, U)(T first, U second) if (isIntegral!T &&
 isFloatingPoint!U)
 {
      return false;
 }
 bool testRoundTrip(U, T)(U first, T second) if (isIntegral!T &&
 isFloatingPoint!U)
 {
      return false;
 }

 They look to me to be distinct functions, one with an integral parameter
 followed by a floating point parameter and the other with a floating
 point parameter followed by an integral parameter.

 At first I thought this was a template problem, but the following code
 also raises the same error.

 bool testRoundTrip(int first, double second)
 {
      return false;
 }
 bool testRoundTrip(double first, int second)
 {
      return false;
 }

 What's the problem?
Cannot reproduce on either 2.065 or git head (according to dpaste). import std.traits; bool testRoundTrip(T, U)(T first, U second) if (isIntegral!T && isFloatingPoint!U) { return first == 1; } bool testRoundTrip(U, T)(U first, T second) if (isIntegral!T && isFloatingPoint!U) { return second == 2; } void main(){ int x = 1; double y = 1.1; assert(testRoundTrip(x, y)); x = 2; y = 2.2; assert(testRoundTrip(y, x)); assert(testRoundTrip(1, 1.1)); assert(testRoundTrip(2.2, 2)); }
Aug 09 2014
parent "Paul D Anderson" <claude.rains msn.com> writes:
On Saturday, 9 August 2014 at 07:07:42 UTC, Rikki Cattermole 
wrote:
 Cannot reproduce on either 2.065 or git head (according to 
 dpaste).
You are right. I had the functions in a unittest block that got executed more than once so the second execution was a redefinition. Thanks for taking the time to check. Paul
Aug 09 2014