[Development] How to port from Q_FOREACH to range-based for

Nicolas Arnaud-Cormos nicolas.arnaud-cormos at kdab.com
Wed Jun 12 07:54:29 CEST 2019


On 11/06/2019 21:51, André Pönitz wrote:
> On Tue, Jun 11, 2019 at 08:47:12PM +0200, Mutz, Marc via Development wrote:
>> On 2019-06-11 09:48, Lars Knoll wrote:
>>>> On 11 Jun 2019, at 09:35, Olivier Goffart <olivier at woboq.com> wrote:
>>>>
>>>> On 11.06.19 09:17, Lars Knoll wrote:
>>>>> So, is removing it worth all the hassle to us and our users?
>>>>> Q_FOREACH is a macro and it doesn’t really cost us anything to
>>>>> keep it around. Yes, it has issues with non Qt containers and I
>>>>> wouldn’t recommend it for any new code. But maybe we could
>>>>> simply fix that part, but making Q_FOREACH emit a compiler
>>>>> warning if used on a container that’s not implicitly shared?
>>>> +1
>>>> Although we should probably still discourage its usage in the
>>>> documentation.
>>>>
>>>> Regarding the compiler warning:
>>>>   https://codereview.qt-project.org/c/qt/qtbase/+/244010
>>>>
>>> Nice. So doesn’t this solve most of the issues we have with Q_FOREACH
>>> (maybe with the exception that some people find macros ugly)?
>> If you look at the git history, you will find that it's not at all
>> maintenance-free code that just sits there. I just saw a C++17(!)-only code
>> path there when I rebased my deprecation patch to current dev the other day.
>>
>> There's also, as Peppe has repeatedly indicated (and never got a proper
>> reply to) the teaching/documentation issue: as long as Q_FOREACH stays,
>> undeprecated, we need to teach people about it: when to use and when not to
>> use.
>> That's usually 10min or so of an intro Qt course that could be better
>> spent on teaching something more important.
> It takes less 10 minutes to say "Use ranged for, but do not touch foreach
> unless you know what you are doing".
Sorry, that's not teaching.
You need to explain the reasons for using foo or bar, and convince the 
students that what you are saying is the right way. It does take more 
than 10 mins.

Whenever Qt is adding a new feature there's a teachability issue that 
needs to be handled. Any duplication with the C++ standard adds 
cognitive load to students.
One could dismiss that by saying just use the Qt solution, but it would 
be hiding himself in a box: I'm working on customer codes (not mine), 
I've never seen any code not using the STL.

Btw, the C++ has got recently some issues with teachability with all the 
new features, that's the reason of the creation of WG21.

Regards,
Nicolas



More information about the Development mailing list