[ACCEPTED]-C++11 range-based for on a vector of pointers-gcc

Accepted answer
Score: 20
for ((int*) &p : values)

This is wrong. (int*) is an expression alone, so 5 you need to do int*& (with no parenthesis, that 4 makes an expression - aka "not a type name") at 3 least to make it correct. I prefer to use 2 auto or auto&, personally.

You can do 1 :

for (auto p : values) // here p is a pointer, a copy of each pointer


for (auto& p : values ) // here p is a non-const reference to a pointer


for ( int* p : values ) // here p is a copy of each pointer

or in generic code:

for ( auto&& p: values ) // p is either a const reference to what is in values, or a non-const reference, depends on the context
Score: 1

I think you meant to iterate over 'pointers' instead 1 of 'values' there...

More Related questions