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?
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.
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.
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.
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).
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...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With