I am trying to convert std::vector<T>::iterator
to void *
, but getting compiler error as wrong conversion. is there any way?
Asked
Active
Viewed 1,983 times
-1

Barry
- 286,269
- 29
- 621
- 977

Dr. Debasish Jana
- 6,980
- 4
- 30
- 69
-
2Can you show the code and the applicable error? – NathanOliver May 02 '16 at 14:03
-
5No, there's no way to do that. If you describe the problem you're trying to solve by doing this, you might get a working solution to the actual problem. (This looks like an [XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem)). – molbdnilo May 02 '16 at 14:04
-
2Please provide a [mcve]. – Barry May 02 '16 at 14:06
-
1If the compiler is complaining then you are probably doing something very bad. – Martin York May 02 '16 at 15:14
1 Answers
4
Just get the pointer to the vector element with dereference:
vector<Type>::iterator i = ...;
void* data = &*i;
Or to vector data:
void* data = vec.data();

Peter K
- 1,787
- 13
- 15
-
3
-
1@ForceBru or even drop the cast : every pointer is implicitly convertible to (but not from) `void*`. – Quentin May 02 '16 at 14:23
-
`struct evil { char* operator&() const { return nullptr; } }; std::vector
`: `&` is unreliable in generic code. – Yakk - Adam Nevraumont May 02 '16 at 20:00 -
Yakk, see this question for discussion on generic addressof: http://stackoverflow.com/questions/6494591/how-can-i-reliably-get-an-objects-address-when-operator-is-overloaded – Peter K May 03 '16 at 13:16