我已经学会了重载operator<以使自定义类与 STL 算法兼容,就像这样:
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()));
但是,当我尝试使用JUCE 库中的ValueTree对象做同样的事情时,它失败了:
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'    
任何人都可以看到我的operator<功能有什么问题吗?
我知道答案可能与 的内部运作有关ValueTree,因此这是一个不完美的问题。但我不知道这里可能出错的类型。在我看来,这两个案例似乎完全对称,所以我不知道为什么一个失败而另一个成功。
请注意:我知道我的数组未排序,set_difference因此会引发异常。现在我只是想让代码编译,并保持示例简短。
要被 ADL 找到,您必须将运算符放在与类相同的命名空间中:
namespace juce
{
    bool operator< (const ValueTree& lhs, const ValueTree& rhs) { /*..*/ }
}
| 归档时间: | 
 | 
| 查看次数: | 69 次 | 
| 最近记录: |