[Interest] Some Qt classes (QUuid) do not work with std C++ containers (unordered_map)? [C++ question]

Till Oliver Knoll till.oliver.knoll at gmail.com
Tue Jul 6 11:17:36 CEST 2021


Dear all,

Disclaimer: my C++ knowledge is a bit rusty, especially as far as
standard C++ containers and modern features (C++11, 17, 20) are
concerned. That's exactly the reason why I have started another small
open source project, and also the reason why I now tend to use C++
standard containers (instead of the Qt ones - for no other reason to get
more knowledge about them).

Every then and when I stumble over some Qt classes which do not work
with some given C++ container classes. In some cases I could explain
this for myself based on some "web research", e.g. the "shared data"
concept of e.g. QString does not work with the concept of "unique
pointers" (which are to be stored in some C++ container. And I
intuitively understand that.

But now I stumbled over the following example for which I do not find
any relevant "web research" results (specific to Qt classes). I am
trying to create an unordered hashmap, with QUuid as key, as in:

#include <unordered_map> #include <QUuid> ... // Won't compile
std::unordered_map<QUuid, QString> exportPlugins;

The compiler (I am currently using the default compiler being used on
macOS 11.4 "Big Sur", with the open source Qt 5.12.2) is complaining
with several error messages (see below for complete error messages), but
they seem to be related to an "implicitly-deleted default constructor"

/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.3.sdk/usr/include/c++/v1/unordered_map:435:
Fehler: call to implicitly-deleted default constructor of
'std::__1::hash<QUuid>'

which seems to be caused by some explicitly deleted default constructor:

/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.3.sdk/usr/include/c++/v1/utility:1564:5:
note: '__enum_hash' has been explicitly marked deleted here

Looking at the QUuid class declaration I do not find anything suspicious
at first glance. Specifically I do see a default constructor (but I do
understant that the error message is referring to some other default
constructor of __enum_hash, in the <utility> header). In fact, when I
exchange QUuid with QString, as in:

// This works std::unordered_map<QString, QString> exportPlugins;

then everything compiles fine.

This is with a CMake based project, with

set(CMAKE_CXX_STANDARD17)

set(CMAKE_CXX_STANDARD_REQUIREDON)

So this should be using a C++17 compliant compiler.

Questions:

I can use a QString instead of a QUuid here just fine (or use some Qt
container class here instead, namely QMap), but I am curious about
"what's going on here":

* Is this a known limitation of QUuid (and possibly other classes), that
it cannot be used in C++ container classes?

* Or asked differently: why does the "template expansion" (?) get into
the case of __enum_hash (when using QUuid), which apparently has an
explicitly deleted default constructor (or why is the default
constructor of QUuid not sufficient)?

* Is there a way to tell (given the Qt's class declaration) whether a Qt
class is suitable for C++ container classes?

* Or am I doing something wrong here (wrong compiler, wrong project
settings, missing includes, ...)?

As this is most likely a basic C++ noob question I am fine with any RTFM
answers here :) But again, so far I did not find anything related to
QUuid and C++ container classes on the web.

Thank you so much,

Oliver


For reference here are the two error messages that I get with QUuid as
key for unordered_map:

#1

/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.3.sdk/usr/include/c++/v1/unordered_map:435:
Fehler: call to implicitly-deleted default constructor of
'std::__1::hash<QUuid>' In file included from
/Users/tknoll/resources/work/Qt/SkyDolly/src/Plugin/src/PluginManager.cpp:26:
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.3.sdk/usr/include/c++/v1/unordered_map:435:11:
error: call to implicitly-deleted default constructor of
'std::__1::hash<QUuid>' : _Hash() {} ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.3.sdk/usr/include/c++/v1/memory:1956:3:
note: in instantiation of member function
'std::__1::__unordered_map_hasher<QUuid,
std::__1::__hash_value_type<QUuid, const QString>,
std::__1::hash<QUuid>, true>::__unordered_map_hasher' requested here
__compressed_pair_elem(__default_init_tag) {} ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.3.sdk/usr/include/c++/v1/unordered_map:906:5:
note: in instantiation of member function
'std::__1::__hash_table<std::__1::__hash_value_type<QUuid, const
QString>, std::__1::__unordered_map_hasher<QUuid,
std::__1::__hash_value_type<QUuid, const QString>,
std::__1::hash<QUuid>, true>, std::__1::__unordered_map_equal<QUuid,
std::__1::__hash_value_type<QUuid, const QString>,
std::__1::equal_to<QUuid>, true>,
std::__1::allocator<std::__1::__hash_value_type<QUuid, const
QString>>>::__hash_table' requested here unordered_map() ^
/Users/tknoll/resources/work/Qt/SkyDolly/src/Plugin/src/PluginManager.cpp:37:5:
note: in instantiation of member function
'std::__1::unordered_map<QUuid, const QString, std::__1::hash<QUuid>,
std::__1::equal_to<QUuid>, std::__1::allocator<std::__1::pair<const
QUuid, const QString>>>::unordered_map' requested here
PluginManagerPrivate() noexcept ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.3.sdk/usr/include/c++/v1/utility:1570:36:
note: default constructor of 'hash<QUuid>' is implicitly deleted because
base class '__enum_hash<QUuid>' has a deleted default constructor struct
_LIBCPP_TEMPLATE_VIS hash : public __enum_hash<_Tp> ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.3.sdk/usr/include/c++/v1/utility:1564:5:
note: '__enum_hash' has been explicitly marked deleted here
__enum_hash() = delete; ^

#2

/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.3.sdk/usr/include/c++/v1/__hash_table:869:
Fehler: static_assert failed due to requirement 'integral_constant<bool,
false>::value' "the specified hash does not meet the Hash requirements"
In file included from
/Users/tknoll/resources/work/Qt/SkyDolly/src/Plugin/src/PluginManager.cpp:26:
In file included from
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.3.sdk/usr/include/c++/v1/unordered_map:411:
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.3.sdk/usr/include/c++/v1/__hash_table:869:5:
error: static_assert failed due to requirement 'integral_constant<bool,
false>::value' "the specified hash does not meet the Hash requirements"
static_assert(__check_hash_requirements<_Key, _Hash>::value, ^
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.3.sdk/usr/include/c++/v1/__hash_table:884:1:
note: in instantiation of template class
'std::__1::__enforce_unordered_container_requirements<QUuid,
std::__1::hash<QUuid>, std::__1::equal_to<QUuid>>' requested here
typename __enforce_unordered_container_requirements<_Key, _Hash,
_Equal>::type ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.3.sdk/usr/include/c++/v1/unordered_map:971:30:
note: while substituting explicitly-specified template arguments into
function template '__diagnose_unordered_container_requirements'
static_assert(sizeof(__diagnose_unordered_container_requirements<_Key,
_Hash, _Pred>(0)), ""); ^
/Users/tknoll/resources/work/Qt/SkyDolly/src/Plugin/src/PluginManager.cpp:37:5:
note: in instantiation of member function
'std::__1::unordered_map<QUuid, const QString, std::__1::hash<QUuid>,
std::__1::equal_to<QUuid>, std::__1::allocator<std::__1::pair<const
QUuid, const QString>>>::~unordered_map' requested here
PluginManagerPrivate() noexcept ^

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20210706/f3bfb29a/attachment-0001.html>


More information about the Interest mailing list