C++11 range-based for on a vector of pointers

for ((int*) &p : values)

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

You can do :

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

or

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

or

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

Tags:

C++

Pointers

Gcc