[Bug 27795] New: Problems using Tp::SharedPtr as a key in QMap

bugzilla-daemon at freedesktop.org bugzilla-daemon at freedesktop.org
Thu Apr 22 18:49:47 CEST 2010


https://bugs.freedesktop.org/show_bug.cgi?id=27795

           Summary: Problems using Tp::SharedPtr as a key in QMap
           Product: Telepathy
           Version: unspecified
          Platform: Other
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: medium
         Component: tp-qt4
        AssignedTo: telepathy-bugs at lists.freedesktop.org
        ReportedBy: jonathon at quotidian.org
         QAContact: telepathy-bugs at lists.freedesktop.org


Created an attachment (id=35238)
 --> (https://bugs.freedesktop.org/attachment.cgi?id=35238)
patch to add operator< and qHash

When you try to use a SharedPtr in a QMap as follows:
  QMap<Tp::SharedPtr<T>, int>

The compiler will happily allow you to do so.  However, when running the
application, you'll notice some strange behavior.  note that QMap (like
std::map) requires that the type used for a key have operator< defined in order
to determine the order of th eelements in the map.  tp-qt4 does not provide
operator< for SharedPtrs, so the compiler looks for a suitable comparison
operator and notices that SharedPtr can be implicitly converted to bool (due to
the unfortunate definition of 'SharedPtr::operator bool()') and so it uses the
following operator to determine element order:
  operator<(bool a, bool b)

So when you insert a new element into the map, it will compare the new key with
the old key by first converting them to booleans.  The impact of this is that
any non-null sharedptr will be interpreted as being the exact same key value. 
This means that the maximum number of elements in this map is 2: one non-null
SharedPtr and one null SharedPtr.  A user can define an appropriate operator<
herslef, but it's not immediately obvious that this is necessary.  It would be
good if the library itself provided the comparison operator.  I'm providing a
patch that adds a few comparison operators and a qHash() implementation for
SharedPtr.

Note that this does not fix all of the unfortunate consequences of defining the
'operator bool()', since the following line will still happily compile despite
it being quite obviously nonsense:
  int i = SharedPtr<T>(new T());

But that's a different bug report...

-- 
Configure bugmail: https://bugs.freedesktop.org/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the QA contact for the bug.
You are the assignee for the bug.



More information about the telepathy-bugs mailing list