0

There is a ThirdParty class that has member variable std::vector<T> vec. Unfortunately it provides only readonly access to it: const std::vector<T>& getVec() const;
I have a temporary object of this class and want to extract this vector. Can I somehow cast away the constness and move the vector instead of copying it? Is it safe?

I have a source code of the ThirdParty class and can check for implementation details if needed.

Dmitry J
  • 867
  • 7
  • 20
  • If you have the source code, why not add a member that returns an rvalue reference to the vector so you can cleanly move-construct your vector? – cdhowie Jul 24 '17 at 21:06
  • 1
    I'd recommend that you just make the copy, unless you've already profiled and found this copy to be a bottleneck in your application. It's best to use 3rd party APIs as they are intended – Justin Jul 24 '17 at 21:06
  • @cdhowie, It's a huge library and I'm kind of afraid of changing its source code... – Dmitry J Jul 24 '17 at 21:07
  • Very related: https://stackoverflow.com/q/357600/1896169 – Justin Jul 24 '17 at 21:07
  • Who can say if it is safe without examining the third party code? The owning object may need all the bits left where it put them. – user4581301 Jul 24 '17 at 22:20

0 Answers0