[solved] Element comparator for ValueTree?

I would like to be able to run STL algorithms on Arrays of ValueTrees, something like this:

Array<ValueTree> one, two, result;

one.sort();
two.sort();

std::set_difference(one.begin(), one.end(),
                    two.begin(), two.end(),
                    backInserter(result));

But in order to do this, I would need to implement operator< for ValueTree, and a similar Element Comparator for the Array::sort(). Is there any sensible way of doing this?

I’m guessing that this is a dead end, but my code would be a lot simpler if I could get it to work.

Still stuck on this stupid problem. I’ve got the ElementComparator working, but I’m not having any luck with the STL algorithm.

struct CompareVTs   // Element Comparator for Array::sort
{
    inline int compareElements(const ValueTree& a, const ValueTree& b)
    {
        const auto parent = a.getParent();

        jassert(parent == b.getParent());

        const auto aIndex = parent.indexOf(a),
                   bIndex = parent.indexOf(b);

        return aIndex < bIndex ? -1 : aIndex == bIndex ? 0 : 1;
    }
};


inline bool operator< (const ValueTree& a, const ValueTree& b) // Why doesn't set_difference() find this?
{
    const auto parent = a.getParent();

    jassert(parent == b.getParent());

    const auto aIndex = parent.indexOf(a),
               bIndex = parent.indexOf(b);

    return aIndex < bIndex;
}


Array<ValueTree> one, two, result;

one.sort();
two.sort();

std::set_difference(one.begin(), one.end(),
                    two.begin(), two.end(),
                    backInserter(result));  // compiler error: failed to specialize function template for std::less

Can anyone see why std::set_difference() isn’t finding my operator< function?

In case anyone else runs into a similar problem, all you have to do is wrap the operator< function in a juce namespace bracket. I lost hours of my life over this!

1 Like