Is it possible to move a vector<T*>
to a vector<const T*>
without copying it and without relying on reinterpret_cast<>
? I.e.
vector<int*> get() {
return ...;
}
vector<const int*> getConst() {
return whatgoeshere(get());
}
Yes, because std::vector is a value-type rather than a reference type. To simplify things: An std::vector considers the values in its buffer as part of itself, so that changing them means changing the vector.
You can't put items into a const vector, the vectors state is the items it holds, and adding items to the vector modifies that state. If you want to append to a vector you must take in a non const ref. Show activity on this post. If you have a const vector it means you can only read the elements from that vector.
If the vector itself is declared const (as in const std::vector<T*>), then you can't modify the vector, but you can modify the objects. If the pointers are declared const (as in std::vector<const T*>), then you can modify the vector, but not the objects.
You "can" use a vector over a queue, if the queue lifetime is short or if you know the maximum size of your queue.
I'm going to attack this from another angle. And address a possible design issue. You didn't specify what comes in the ...
, but assuming get
populates a vector and then returns it, the solution in my view is to lift the code that does the populating outside of both functions.
template<typename Int>
void do_get(std::vector<Int*>& v) {
// Populate v
}
auto get() {
std::vector<int*> ret;
do_get(ret);
return ret;
}
auto getConst() {
std::vector<const int*> ret;
do_get(ret);
return ret;
}
One source of truth for the populating logic. And while the two original functions are identical, it's negligible. Furthermore on a sane implementation it won't do any superfluous copies, because RVO is amazing.
No.
Although a T*
may be trivially converted to a const T*
, a container of T*
is not "related" to a container of const T*
, so there is simply no functionality to do what you ask.
Consider also that such functionality might hypothetically assign a int**
to a const int**
, which is not permitted (there is no special case provided for when the programmer intended this assignment to take place as part of a swap operation, as far as I know).
Furthermore, a reinterpret_cast
would merely be hacking over these facts, giving your program undefined behaviour.
You are stuck with three options:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With