October 01, 2014
https://issues.dlang.org/show_bug.cgi?id=13566

          Issue ID: 13566
           Summary: std.algorithm.cmp treats string length as element
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: major
          Priority: P1
         Component: DMD
          Assignee: nobody@puremagic.com
          Reporter: acehreli@yahoo.com

The following code cannot be compiled on a 64-bit environment:

import std.algorithm;

bool myPred(dchar a, dchar b)
{
    return false;
}

void main()
{
    cmp!myPred("", "");
}

phobos/std/algorithm.d(7314): Error: myPred (dchar a, dchar b) is not callable
using argument types (ulong, ulong)
phobos/std/algorithm.d(7314): Error: myPred (dchar a, dchar b) is not callable
using argument types (ulong, ulong)
deneme.d(179975): Error: template instance std.algorithm.cmp!(myPred, string,
string) error instantiating

A quick investigation reveals that one of the overloads of threeWay() inside std.algorithm.cmp takes two size_t parameters that are supposed to be string lengths. Unfortunately, threeWay() then tries to pass those lengths to the predicate:

    // For speed only
    static int threeWay(size_t a, size_t b)
    {
        static if (size_t.sizeof == int.sizeof && isLessThan)
            return a - b;
        else
            return binaryFun!pred(b, a) ? 1 : binaryFun!pred(a, b) ? -1 : 0; //
<-- HERE
    }

I've also noticed that unittests of cmp() don't consider special predicates at
all. ;)

Ali

--