Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GCC 4.4 does not implement C++11 range loop. What other range loop syntax does it support?

Tags:

c++

c++11

g++

I have some third party tool that uses some c++11 features and I'm needing to compile it under an gcc 4.4. As I'm not at all familiar with c++11 new features yet I thought I'd ask for help after my google search turned up fruitless.

I've enabled c++0x switch but it doesn't help here:

for (auto const& fixup : self->m_Fixups)

The error produced is:

error: expected initializer before ':' token

What other range loop syntax which functions equivalently to C++11 range loop does GCC 4.4 support?

like image 266
hookenz Avatar asked Jan 15 '14 23:01

hookenz


People also ask

Does C have range-based for loops?

Range-based for loop in C++ It executes a for loop over a range. Used as a more readable equivalent to the traditional for loop operating over a range of values, such as all elements in a container.

How do range-based for loops work C++?

A range-based for loop terminates when one of these in statement is executed: a break , return , or goto to a labeled statement outside the range-based for loop. A continue statement in a range-based for loop terminates only the current iteration.

What is for each loop in C++?

Foreach loop is used to iterate over the elements of a containers (array, vectors etc) quickly without performing initialization, testing and increment/decrement. The working of foreach loops is to do something for every element rather than doing something n times.

Are range-based for loops faster?

Range-for is as fast as possible since it caches the end iterator[citationprovided], uses pre-increment and only dereferences the iterator once. Then, yes, range-for may be slightly faster, since it's also easier to write there's no reason not to use it (when appropriate).


1 Answers

The code is a range-based for-loop which is indeed new in C++11. It is not implemented in GCC 4.4, unlike some other features from C++11. Try this:

for( auto it = self->m_Fixups.begin(); it != self->m_Fixups.end(); ++it )
{
    const auto& fixup = *it;
    // the rest of the code...
}

The above uses some C++11 features which should be available in GCC 4.4.


As Ben Voigt pointed out: If you need to make the code more efficient, you might also use this slightly less concise version:

for( auto it = self->m_Fixups.begin(), end = self->m_Fixups.end(); it != end; ++it )
{
    const auto& fixup = *it;
    // the rest of the code...
}
like image 111
Daniel Frey Avatar answered Oct 05 '22 17:10

Daniel Frey