[Interest] QVariant compare operator

André Pönitz apoenitz at t-online.de
Sat Apr 18 18:54:32 CEST 2020


On Sat, Apr 18, 2020 at 04:53:04PM +0200, Bernhard Lindner wrote:
> Hi!
> 
> > > > Relational comparisons with QVariant are deprecated in 5.15 and will be
> > > > removed because they are a misfeaure.
> > > > Redesign your code so your question does not need to be asked.
> > > 
> > > Could you please explain why comparing two QVariants is a misfeature?
> > 
> > Not comparing for equality. Comparing for ordering.
> >
> > What comes first, QSize(1, 1) or QRegularExpression("^$") ?
> 
> Well, if QVariant::canConvert() says right operand can be converted to type of left, then
> convert and compare the result.

The main problem with QVariant conversions is that in the way they are implemented
currently does not make them suitable even for equality comparision, leading to
effects like 

    #include <QMap>
    #include <QDebug>

    int main()
    {
        QVariant v1 = QString("a");
        QVariant v2 = QChar('a');
        QVariant v3 = QByteArray("a");

        qDebug() << QMap<QVariant, int>{{v1, 0}, {v2, 0}, {v3, 0}}.size();
        qDebug() << QMap<QVariant, int>{{v2, 0}, {v1, 0}, {v3, 0}}.size();
    }

producing

   1
   2

In theory, that's solvable, in practice you will always find someone adding yet
another conversion for "convenience" breaking that again.

> If it can't convert, behavior should be undefined and
> qFatal() should be called (or whatever Qt 5.15 prefers to do in detectable cases of
> undefined behavior).

The problem are not missing conversions, but too many conversions.

Andre'


More information about the Interest mailing list