具有迭代器本身的STL容器:set vs unordered_set

时间:2018-10-14 13:46:43

标签: c++ templates stl unordered-set

我想创建一个包含数据的容器,该数据包括容器本身的迭代器。简化的代码如下。它适用于大多数STL容器(vectorset等),但不适用于unordered_set进行编译,在检查哈希要求时会出现一些神秘的编译错误。

#include <set>
#include <unordered_set>

template <class C, class T>
struct rebind {};

template <class T0, class T>
struct rebind<std::set<T0>, T> {
    using type = std::set<T>;
};

template <class T0, class T>
struct rebind<std::unordered_set<T0>, T> {
    using type = std::unordered_set<T>;
};

template <class C>
class wrapped_iterator;

template <class C>
using twisted_container = typename rebind<C, wrapped_iterator<C>>::type;

template <class C>
class wrapped_iterator : public twisted_container<C>::iterator {
    size_t key() const { return reinterpret_cast<std::uintptr_t>(std::addressof(**this)); }
    bool operator < (const wrapped_iterator& other) const { return key() < other.key(); }
    bool operator == (const wrapped_iterator& other) const { return key() == other.key(); }
};

namespace std {

template <class C>
struct hash<wrapped_iterator<C>> {
    size_t operator()(wrapped_iterator<C> it) const {
        return it.key();
    }
};

}

int main() {
    twisted_container<std::set<int>> a;
    twisted_container<std::unordered_set<int>> b;  // This doesn't compile!
    return 0;
}

c语错误:

<source>:24:59: error: no type named 'iterator' in 'std::unordered_set<wrapped_iterator<std::unordered_set<int, std::hash<int>, std::equal_to<int>, std::allocator<int> > >, std::hash<wrapped_iterator<std::unordered_set<int, std::hash<int>, std::equal_to<int>, std::allocator<int> > > >, std::equal_to<wrapped_iterator<std::unordered_set<int, std::hash<int>, std::equal_to<int>, std::allocator<int> > > >, std::allocator<wrapped_iterator<std::unordered_set<int, std::hash<int>, std::equal_to<int>, std::allocator<int> > > > >'
    class wrapped_iterator : public twisted_container<C>::iterator {
                                    ~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~

gcc错误:

<source>:24:11: error: invalid use of incomplete type 'twisted_container<std::unordered_set<int> >' {aka 'class std::unordered_set<wrapped_iterator<std::unordered_set<int> >, std::hash<wrapped_iterator<std::unordered_set<int> > >, std::equal_to<wrapped_iterator<std::unordered_set<int> > >, std::allocator<wrapped_iterator<std::unordered_set<int> > > >'}
     class wrapped_iterator : public twisted_container<C>::iterator {
           ^~~~~~~~~~~~~~~~

我要尝试做违法的事情吗?有没有办法解决此错误?为什么unordered_set在这种情况下特别?是编译器实现中的预期行为还是错误?

0 个答案:

没有答案