在QHash或QSet中使用标准库类型作为键

时间:2017-11-23 16:42:24

标签: c++ qt

Qt要求在Qt的qHash模板中使用密钥类型的QHash重载。根据{{​​3}},这种重载需要在类型命名空间中进行#34;"。但这是一个问题,因为在c ++中为std命名空间the documentation添加了新的重载。只是将重载添加到全局命名空间也不起作用。

一个最小的例子:

#include <QHash>
#include <string>

//namespace std { // when adding to namespace std it compilies but that is not allowed
    static uint qHash(const std::u32string &key, uint seed) noexcept {
        return static_cast<uint>(std::hash<std::u32string>{}(key));
    }
//}

QHash<std::u32string, int> h;

int main(int argc, char **argv) {
    h.insert(std::u32string(), 5);
}

生成的错误消息相当长,我省略了尝试的候选列表(它们不包含字符串重载)

In file included from /usr/include/x86_64-linux-gnu/qt5/QtCore/qglobal.h:83:0,
                 from /usr/include/x86_64-linux-gnu/qt5/QtCore/qcoreapplication.h:43,
                 from /usr/include/x86_64-linux-gnu/qt5/QtCore/QCoreApplication:1,
                 from ../test.cpp:1:
/usr/include/x86_64-linux-gnu/qt5/QtCore/qhashfunctions.h: In instantiation of ‘uint qHash(const T&, uint) [with T = std::__cxx11::basic_string<char32_t>; uint = unsigned int]’:
/usr/include/x86_64-linux-gnu/qt5/QtCore/qhash.h:920:18:   required from ‘QHash<K, V>::Node** QHash<K, V>::findNode(const Key&, uint*) const [with Key = std::__cxx11::basic_string<char32_t>; T = int; QHash<K, V>::Node = QHashNode<std::__cxx11::basic_string<char32_t>, int>; uint = unsigned int]’
/usr/include/x86_64-linux-gnu/qt5/QtCore/qhash.h:760:27:   required from ‘QHash<K, V>::iterator QHash<K, V>::insert(const Key&, const T&) [with Key = std::__cxx11::basic_string<char32_t>; T = int]’
../qttui/testtui2.cpp:15:33:   required from here
/usr/include/x86_64-linux-gnu/qt5/QtCore/qhashfunctions.h:110:40: error: no matching function for call to ‘qHash(const std::__cxx11::basic_string<char32_t>&)’
     Q_DECL_NOEXCEPT_EXPR(noexcept(qHash(t)))
                                   ~~~~~^~~
/usr/include/x86_64-linux-gnu/qt5/QtCore/qcompilerdetection.h:1144:43: note: in definition of macro ‘Q_DECL_NOEXCEPT_EXPR’
 # define Q_DECL_NOEXCEPT_EXPR(x) noexcept(x)
                                           ^
In file included from /usr/include/x86_64-linux-gnu/qt5/QtCore/qlist.h:47:0,
                 from /usr/include/x86_64-linux-gnu/qt5/QtCore/qobject.h:49,
                 from /usr/include/x86_64-linux-gnu/qt5/QtCore/qcoreapplication.h:46,
                 from /usr/include/x86_64-linux-gnu/qt5/QtCore/QCoreApplication:1,
                 from test.cpp:4,
/usr/include/x86_64-linux-gnu/qt5/QtCore/qhashfunctions.h:72:52: note: candidate: constexpr uint qHash(char, uint)
 Q_DECL_CONST_FUNCTION Q_DECL_CONSTEXPR inline uint qHash(char key, uint seed = 0) Q_DECL_NOTHROW { return uint(key) ^ seed; }
                                                    ^~~~~
/usr/include/x86_64-linux-gnu/qt5/QtCore/qhashfunctions.h:72:52: note:   no known conversion for argument 1 from ‘const std::__cxx11::basic_string<char32_t>’ to ‘char’

错误消息中的代码是这样的(据我所知,在全局命名空间中):

template<typename T> inline uint qHash(const T &t, uint seed)
    Q_DECL_NOEXCEPT_EXPR(noexcept(qHash(t))) // <---- qhashfunctions.h:110
{ return qHash(t) ^ seed; }

template <class Key, class T>
Q_OUTOFLINE_TEMPLATE typename QHash<Key, T>::Node **QHash<Key, T>::findNode(const Key &akey,                                                                           uint *ahp) const
{
    uint h = 0;

    if (d->numBuckets || ahp) {
        h = qHash(akey, d->seed); // <---- qhash.h:920
        if (ahp)
            *ahp = h;
    }
    return findNode(akey, h);
}

1 个答案:

答案 0 :(得分:2)

将重载放入正在进行查找的Qt命名空间中。

更具体地说,必须将其注入到发生ADL查找尝试的命名空间中。这可能涉及跟踪错误发生在哪个命名空间。

如果您正在设计库并希望避免此问题,则应专门创建一个名称空间来解决此问题。

创建两个功能:internal_qHashqHash

namespace mylibrary {
  namespace hash_support {
    struct qhash_tag {};
    uint qHash( qhash_tag, int const& t, uint seed ) { /* TODO */ }
  }
  using ::mylibrary::hash_support::qhash_tag;
  template<class T>
  constexpr uint internal_qHash( T const& t, uint seed) {
    using ::mylibrary::hash_support::qHash;
    return qHash( qhash_tag{}, t, seed );
  }
  namespace hash_adl_blocking {
    template<class T>
    constexpr uint qHash( T const& t, uint seed ) {
      return ::mylibrary::internal_qHash( t, seed );
    }
  }
  using ::mylibrary::hash_adl_blocking::qHash;
}

现在使用mylibrary::qHash执行基于ADL的mylibrary::hash_support查找以及T的任何关联命名空间。

如果我们想要进行SFINAE检测,还需要做更多的工作。

在此模型中,您将为名称空间创建重载,而不能将函数注入namespace mylibrary::hash_support内部。

Qt可能已经为qHash(int, uint)做了类似的事情。查看Qt定义的位置,如果您在那里定义std重载,它应该可以工作。

qhash_tag强制两阶段名称查找重新考虑在点mylibrary::hash_support注入qHash的新符号,并为给定类型实例化。