reset password
Author Message
kknaur
Posts: 540
Posted 11:10 Jan 18, 2014 |

Hey Class,

I got a question about the availability of for-each loops in C++.  When I took this class, we never covered for-each loops since they were not implemented at the time.  However, with the most recent update of the C++ standard (C++11), for-each loops have been added.

One caveat to using for-each loops is that you actually have to enable a flag in Code::Blocks in order for the compiler to allow their use.  

Go to Settings -> Compiler -> Global Compiler Settings -> Compiler Settings Tab -> then under the Compiler Flags tab click the box which says "Have g++ follow the C++11 ISO C++ language standard."

An example of a for-each loop can be seen here:

#include <iostream>

int main() {

    int myint[] = {1,2,3,4,5};

    for (int i: myint) {
        std::cout << i << std::endl;
    }
}

Code::Blocks uses an open source compiler, G++, from the TDM-GCC version 4.7.1 compiler suite. By default, I believe the compiler supports "the majority of C++98 (export is a notable exception) and most of the changes in C++03" (as quoted from the GCC documentation).  C++11 support can be enabled using its respective flag.  You can read about why this option is not enabled by default here:

Why c11 Compiler Support Still Requires-a-Flag

If you are using Visual Studio 2012, it seems for-each loops work by default so some if not all of the features of the C++11 standard seem to be available by default in the Visual Studio compiler.  Further research is required to see how much of C++11 is supported by VS2012.

Near as I can tell, the textbook covers the C++98/03 standard. So what is added by the C++11 standard is beyond the scope of this class.

If you are interested in what is added by C++11 then you can read more about it here:

Wikipedia Article on C++11 Standard

And the features supported by the different versions of GCC can be found here:

C++11 Support in GCC

As a side note: Code::Blocks supports a wide range of compilers, so if you wish to install and experiment with a compiler other than what is installed with Code::Blocks by default, please feel free to do so.