79664221

Date: 2025-06-13 00:11:27
Score: 0.5
Natty:
Report link

What you could do if copying the data is a performance concern is to use reinterpret_cast to cast c1 to a reference to a vector of the required type:

std::vector<int*> c1;  
std::vector<void*>& c2 = reinterpret_cast<std::vector<void*>&>(c1);

I have to stress, though, that you are relying on the fact that pointers to one type usually are much like pointers to another type. In particular, they are the same size so they are stored in the vector the same way. So this will work as long as the memory layout of the type you're casting to is the same as type you're casting from. There is no guarantee that doing this is OK in your specific case because we don't know the circumstances and why you're trying to do this. For example the two pointer types might have different alignment requirements. So you should normally stick to well defined behavior and copy the vector. You should not be avoiding the copy just because it is not needed.

Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Starts with a question (0.5): What you
  • Low reputation (1):
Posted by: Plover