最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c++ - unordered_set with custom hash function as a struct member gives -Wsubobject-linkage - Stack Overflow

programmeradmin10浏览0评论

I am trying to put an unordered_set<pair<int, int>> as a struct member, which requires a custom hash function. I don't want to overload std::hash<pair<int, int>>, so I tried the following:

test.hpp

#ifndef TEST_HPP
#define TEST_HPP

#include <unordered_set>

struct myStruct {
    constexpr static auto pair_hash = [](const std::pair<int, int>& p) -> size_t {
        return std::hash<int>{}(p.first) ^ (std::hash<int>{}(p.second) << 1);
    };
    std::unordered_set<std::pair<int, int>, decltype(pair_hash)> mySet;
};

#endif

test.cpp:

#include "test.hpp"

int main() {
    myStruct s;
    return 0;
}

This compiles and works, but gives the warning: ‘myStruct’ has a field ‘std::unordered_set<std::pair<int, int>, const myStruct::<lambda(const std::pair<int, int>&)> > myStruct::mySet’ whose type has internal linkage [-Wsubobject-linkage]

What is the problem, and how do I fix it?

发布评论

评论列表(0)

  1. 暂无评论