C++ 为什么clang拒绝gcc接受的这个无序集合定义?

C++ 为什么clang拒绝gcc接受的这个无序集合定义?,c++,hash,compilation,clang,unordered-set,C++,Hash,Compilation,Clang,Unordered Set,我想用我自己的散列函数测试无序集: #include<unordered_set> #include<iostream> #include<functional> using namespace std; struct node{ size_t value; bool operator == (const node& n){return value == n.value;} }; size_t h(const node& n){

我想用我自己的散列函数测试无序集:

#include<unordered_set>
#include<iostream>
#include<functional>
using namespace std;
struct node{
    size_t value;
    bool operator == (const node& n){return value == n.value;}
};
size_t h(const node& n){
    return n.value;
}
int main(){
    unordered_set<node, std::function<size_t(const node&)>> s2(3,h);//failed
    return 0;
}
#包括
#包括
#包括
使用名称空间std;
结构节点{
大小值;
布尔运算符==(常量节点&n){返回值==n.value;}
};
大小\u t h(常数节点&n){
返回n.value;
}
int main(){
无序_集s2(3,h);//失败
返回0;
}
我试着编译它,而clang给出了大量错误:

clang++ m.cpp -std=c++11
In file included from m.cpp:1:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/unordered_set:324:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/functional:659:21: error: invalid operands to binary
    expression ('const node' and 'const node')
        {return __x == __y;}
                ~~~ ^  ~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/__hash_table:2175:32: note: in instantiation of member
    function 'std::__1::equal_to<node>::operator()' requested here
                            key_eq()(__cp->__value_, __np->__next_->__value_);
                            ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/__hash_table:2121:9: note: in instantiation of member function
    'std::__1::__hash_table<node, std::__1::function<unsigned long (const node &)>, std::__1::equal_to<node>, std::__1::allocator<node> >::__rehash' requested here
        __rehash(__n);
        ^
clang++m.cpp-std=c++11
在m.cpp中包含的文件中:1:
在/Applications/Xcode.app/Contents/Developer/Toolchains/xcodefault.xctoolchain/usr/bin//包含的文件中/include/c++/v1/unordered_set:324:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/./include/c++/v1/functional:659:21:错误:二进制操作数无效
表达式('const node'和'const node')
{return}
~~~ ^  ~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/。/include/c++/v1/\uuuuu哈希表:2175:32:注意:在成员的实例化中
此处请求了函数“std::\u 1::equal_to::operator()”
key_eq();
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/./include/c++/v1/\uuuu散列表:2121:9:注意:在成员函数的实例化中
此处请求了“std::uu 1::u哈希表::u rehash”
__再灰化(n);
^

我不太了解这里的错误信息,您能告诉我如何修复代码吗?

您的比较运算符必须是
const
合格的:

bool operator == (const node& n) const {return value == n.value;}
                                 ^^^^^

通过将运算符实现为非成员函数,这样的错误很容易避免。有关更多信息和最佳实践,请参阅。

尽管Baum-mit-Augen已经告诉您了这个问题,但我认为最好也解释一下如何从错误消息中找出更多信息

clang++ m.cpp -std=c++11 In file included from m.cpp:1: In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/unordered_set:324: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/functional:659:21: error: invalid operands to binary expression ('const node' and 'const node') {return __x == __y;} ~~~ ^ ~~~ /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/__hash_table:2175:32: note: in instantiation of member function 'std::__1::equal_to::operator()' requested here key_eq()(__cp->__value_, __np->__next_->__value_); ^ /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/__hash_table:2121:9: note: in instantiation of member function 'std::__1::__hash_table, std::__1::equal_to, std::__1::allocator >::__rehash' requested here __rehash(__n); ^ 如果您尝试编译此文件,clang将为您提供更多详细信息:

error: invalid operands to binary expression ('const node' and 'const node') a == b; ~ ^ ~ note: candidate function not viable: 'this' argument has type 'const node', but method is not marked const bool operator == (const node& n){return value == n.value;} ^ 错误:二进制表达式('const node'和'const node'的操作数无效) a==b; ~ ^ ~ 注意:候选函数不可行:“this”参数的类型为“const node”,但方法未标记为const 布尔运算符==(常量节点&n){返回值==n.value;} ^ “method is not marked const”准确地告诉您问题所在。要修复它,如Baum-mit-Augen的答案所示,请标记方法
const

另一方面,如果答案是“不,您不应该能够比较两个
常量节点
对象”,那么问题将是“为什么
无序设置
比较两个
常量节点
对象以及如何停止它”。为此,初始编译器消息的其余部分将告诉您是哪些部分导致了这种比较。你必须从上到下,在每一步都要弄清楚“这应该有效吗?”如果有效,就弄清楚为什么无效。如果不是,请找出导致尝试的原因

error: invalid operands to binary expression ('const node' and 'const node') a == b; ~ ^ ~ note: candidate function not viable: 'this' argument has type 'const node', but method is not marked const bool operator == (const node& n){return value == n.value;} ^