CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    'for' loops on C++11

    Yesterday I came across this 'for' loop in some C++11 code:-

    Code:
     	for (auto & trig : all_triggers) {
     		trig->process_state_requests (bufs, nframes - 1);
    	}
    Is it just me - or was the older syntax a lot easier to understand?? For example, what does auto & trig mean? And how does it know how much to increment each time around the loop? And how does a loop know when to end?
    "A problem well stated is a problem half solved.” - Charles F. Kettering

  2. #2
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: 'for' loops on C++11

    all_triggers is a type that supports .begin(), increment (++) and .end() . all_triggers is iterated from .begin() to .end() by incrementing an internal iterator. The iterator is de-referenced and passed as the value of the for variable (trig). & means that this value is passed by ref. auto means that the type is automatically determined by the compiler from the type of the de-referenced iterator.

    This is a range-for. See https://en.cppreference.com/w/cpp/language/range-for
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    Re: 'for' loops on C++11

    Thanks 2kaud - that article seems to have quite a few warnings about things not behaving the way you might expect... but the bit that made me smile was this statement:- "Used as a more readable equivalent to the traditional for loop "
    "A problem well stated is a problem half solved.” - Charles F. Kettering

  4. #4
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: 'for' loops on C++11

    Well IMO it's far better than something like:

    Code:
    for (auto itr {all_triggers.begin()}; itr != all_triggers,end(); itr = std::next(itr))
     		(*itr)->process_state_requests (bufs, nframes - 1);
    Also, range-for can work with any class that defines ,begin(), ,end(), operator++() and operator*(). eg I have a CSV class that allows range-for to iterate over the values. Another class that will return the index value (not needed so much since C++20) and whether it's the last value. etc etc by returning a struct. They are very useful.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured