I have learned to overload operator<
in order to make custom classes compatible with STL algorithms, just like this:
struct A
{ int a; };
bool operator< (const A& x, const A& y)
{ return x.a < y.a; }
std::vector<A> aOne, aTwo, aResult;
std::set_difference(aOne.begin(), aOne.end(),
aTwo.begin(), aTwo.end(),
std::inserter(aResult, aResult.begin()));
However, when I try to do the same thing with a ValueTree object from the JUCE library, it fails:
bool operator< (const juce::ValueTree& x, const juce::ValueTree& y)
{
// let's now worry about the implementation of this function here...
return true;
}
std::vector<juce::ValueTree> vOne, vTwo, vResult;
std::set_difference(vOne.begin(), vOne.end(),
vTwo.begin(), vTwo.end(),
std::inserter(vResult, vResult.begin()));
// COMPILER ERROR: Failed to specialize function template 'unknown-type std::less<void>::operator ()(_Ty1 &&,_Ty2 &&) const'
Can anyone see what is wrong with my operator<
function?
I am aware that the answer probably has to do with the inner workings of ValueTree
, and that this is therefore an imperfect question. But I have no idea of the type of thing that could be going wrong here. To my eyes, the two cases seem entirely symmetrical, so I don't know why one would fail while the other succeeds.
Please note: I am aware that I have left my arrays unsorted, and that set_difference
would therefore throw an exception. Right now I'm just trying to get the code to compile, and keeping the example brief.
To be found by ADL, you have to put your operator in the same namespace as the class:
namespace juce
{
bool operator< (const ValueTree& lhs, const ValueTree& rhs) { /*..*/ }
}