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

Accepted answer

```
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
```

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
```

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

Source:
stackoverflow.com

More Related questions

Cookie Warning

We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.