为什么这个运算符<重载函数对 STL 算法不可见?
Why is this operator< overload function invisible to STL algorithms?
我已经学会了重载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) { /*..*/ }
}
相关文章:
- 为什么这个运算符<重载函数对 STL 算法不可见?
- STL算法函数在多个一维容器上的使用
- 如何在 C++17 STL 并行算法中处理调度?
- C++:尝试使用等效的 STL 算法消除原始循环
- STL算法和back_inserter可以预分配空间吗?
- 如何使用 STL 算法将整数向量转换为字符串向量?
- 如何知道用于实现标准代码段的确切数据结构和算法,例如在C++STL中?
- 是否有一种 STL 算法可以最后找到,但它也适用于指针?
- C++/STL 我应该使用哪种算法来检查容器是否有重复项?
- lower_bound() 算法/STL 使用前提条件
- C++ - 如何使用<int> <int>STL 算法功能在矢量<vector>中找到矢量?
- 如何在变量容器上使用stl算法
- 如何通过 stl 容器和算法库计算两个向量的内积?
- 在 stl 算法中移动迭代器
- 如何使用 STL 算法找到最小值和最大值?
- C STL算法相等
- 在 stl 算法中使用函数对象
- 修改算法 STL 中的make_heap C++以用作最小堆
- 非标准条件排序算法STL c++
- 在从算法stl c++进行排序时访问未分配的内存