[Issue 9513] RedBlackTree excessively copies structs by value

d-bugmail at puremagic.com d-bugmail at puremagic.com
Thu Feb 14 16:21:11 PST 2013


http://d.puremagic.com/issues/show_bug.cgi?id=9513



--- Comment #3 from Ivan Kazmenko <gassa at mail.ru> 2013-02-14 16:21:09 PST ---
(In reply to comment #1)
> The second program gives me:
> 
> temp.d(28): Error: template instance RedBlackTree!(element, lessfun)
> RedBlackTree!(element, lessfun) does not match template declaration
> RedBlackTree(T, alias less = "a < b", bool allowDuplicates = false) if
> (is(typeof(binaryFun!(less)(T.init, T.init))))

Hmm, I just tried with the current release, DMD 2.061, and it gives me the same
error.  However, DMD 2.059 works with no complaint.

The problem arises from the constraint in RedBlackTree declaration:
-----
final class RedBlackTree(T, alias less = "a < b", bool allowDuplicates = false)
    if(is(typeof(binaryFun!less(T.init, T.init))))
-----

The function taking ref arguments can not work for T.init and T.init since ref
implies lvalue.

Currently, I found two work-arounds, more or less clumsy.

The first one is to define the second function to avoid the check:
-----
// old one
bool lessfun (ref element a, ref element b)
{
    return a < b;
}

// new one
bool lessfun (element a, element b)
{
    return a < b;
}
-----

Obviously, this way, we need four identical functions to cover all possible
cases.

The second one is to use template and auto ref:
-----
bool lessfun (T) (auto ref T a, auto ref T b)
{
    return a < b;
}
-----

This works fine.  And this could be a suitable solution for the original
problem.  That is, implementing "a < b" in a similar way as the default
predicate for the RedBlackTree!(AnyStructType) container may be not that bad. 
But that makes me wonder: under the hood, does it still create the four
identical functions?

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list