[Development] QDateTime addDays logic

NIkolai Marchenko enmarantispam at gmail.com
Thu Dec 13 14:48:55 CET 2018


This non obvious (from function name) behaviour actually caused infinite
loop regression in our code just recently.
The person used it inside a while loop thinking it will loop upwards and
stop.

On Thu, Dec 13, 2018 at 4:45 PM Edward Welbourne <edward.welbourne at qt.io>
wrote:

> Fausto Papandrea (13 December 2018 12:48)
> > Hi, I would like to understand the logic of the addDays function of
> > QDateTime.
> >
> > I mean, why doesn't it modify the calling object, but returns a copy of
> > a new object instead?
>
> At this point, it does what it does because it's done so for years
> (since 5.0, at least) and changing it would break various compatibility
> promises.  I guess the reason for it originally would be a general
> preference for non-mutating methods; think of the add*() methods as
> operator+() specialisations, rather than as operator+=().  If you need
> to advance a QDateTime's day, you can always use
>
>   when->setDate(when.date().addDays(n));
>
> or simply
>
>   when = when.addDays(n);
>
> to achieve the mutating variant.
>
>         Eddy.
> _______________________________________________
> Development mailing list
> Development at qt-project.org
> https://lists.qt-project.org/listinfo/development
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/development/attachments/20181213/ea7a0890/attachment.html>


More information about the Development mailing list