[Development] QDateTime addDays logic

Dmitriy Purgin dpurgin at gmail.com
Thu Dec 13 15:11:13 CET 2018


The docs [1] clearly state what the method does. Moreover, the method is
const, so it can't mutate the object.

[1] https://doc.qt.io/qt-5/qdatetime.html#addDays

Cheers
Dmitriy

On Thu, Dec 13, 2018 at 3:04 PM NIkolai Marchenko <enmarantispam at gmail.com>
wrote:

> 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
>>
> _______________________________________________
> 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/f087c203/attachment.html>


More information about the Development mailing list