Ranged Based Iteration Syntax for Linux
Posted: Sun Nov 23, 2014 10:45 pm
Hi Blackstorm! (this is for everyone, but Blackstorm is our resident Linux compile expert!)
Because you compile exclusively on Linux, are you able to determine why we cannot do this with GCC, but we can with MSVC?
This one isn't actually such a big deal, because we CAN do this:
And the normal way to iterate through lists (not range based, with the Colon), I noticed you remove the Ampersand in some, so I'm assuming it wouldn't work:
We use the "old fashioned way" in case we need to modify the container, so we wouldn't be able to use a const qualifier.
There's got to be a way to use the reference! We just need to figure it out. This is more of an issue because we use shared_ptr<>. When you iterate through an STL container with a non-referenced itr, it creates a copy of the object to do so, which wastes processing power for copying the object and with shared_ptr<> it does a reference count ++ and then a reference count -- once we're done with that loop.
From McRae:
"If you want to modify the variable (or avoid passing around large data types by value) and the underlying iterator supports it, you should make the loop variable a reference:"
I found a reference for something that may be of value. NOTE: This applied to Range Based For Loops only! (syntax: for (auto itr : some_STL_contrainer) {)
http://en.cppreference.com/w/cpp/language/range-for
Example near the bottom.
For passing by reference, and thus avoiding the copying, use 2 Ampersands (&&).
Blackstorm, can you try this in one of the range based for loops and see if it compiles? If it does, I'll try updating a few and commit and we'll see if it crashes or not.
Because you compile exclusively on Linux, are you able to determine why we cannot do this with GCC, but we can with MSVC?
Code: Select all
for (auto &itr : some_STL_container) {
Code: Select all
for (auto const &itr : some_STL_container) {
Code: Select all
for (auto &itr = some_STL_contrainer.begin(); itr != some_STL_contrainer.end(); ++itr) {
There's got to be a way to use the reference! We just need to figure it out. This is more of an issue because we use shared_ptr<>. When you iterate through an STL container with a non-referenced itr, it creates a copy of the object to do so, which wastes processing power for copying the object and with shared_ptr<> it does a reference count ++ and then a reference count -- once we're done with that loop.
From McRae:
"If you want to modify the variable (or avoid passing around large data types by value) and the underlying iterator supports it, you should make the loop variable a reference:"
I found a reference for something that may be of value. NOTE: This applied to Range Based For Loops only! (syntax: for (auto itr : some_STL_contrainer) {)
http://en.cppreference.com/w/cpp/language/range-for
Example near the bottom.
For passing by reference, and thus avoiding the copying, use 2 Ampersands (&&).
Blackstorm, can you try this in one of the range based for loops and see if it compiles? If it does, I'll try updating a few and commit and we'll see if it crashes or not.