为何自定义哈希器不被g++-14.2.0识别?已提交bug#118230
GCC编译透明哈希/比较器时的静态断言错误问题
该问题已作为bug#118230提交至GCC Bugzilla。
我尝试使用std::unordered_set::emplace编写了如下代码:
#include <string> #include <unordered_set> struct S { struct Hasher_Cmper { using is_transparent = int; /* Hash */ auto operator()(const auto& s) const noexcept { return std::hash<std::string>{}(std::string{s}); } /* KeyEqual */ bool operator()(const auto& a, const auto& b) const noexcept { return std::string{a} == std::string{b}; } }; std::unordered_set<std::string, Hasher_Cmper, Hasher_Cmper> set; void test() { this->set.emplace("123"); } }; int main() {}
但g++-14.2.0编译器持续报错:
static assertion failed: hash function must \ be invocable with an argument of key type: static_assert( __is_invocable<const _Hash&, const _Key&>{}, ...
请问S::Hasher_Cmper{}为何无法接收std::string类型参数调用?
另外,当仅用单个auto作为operator()(Key)的返回类型时,g++有时能正常编译,我已将该问题上报至GCC。
内容的提问来源于stack exchange,提问作者shynur
相关产品推荐
相关产品推荐

