0

In C++ I can use const_cast to convert a const Eigen::VectorXd& object to an Eigen::VectorXd& one. I wonder if it's possible to do the same with Eigen::Ref objects, i.e., is there a way to convert a const Eigen::Ref<const Eigen::VectorXd>& object to an Eigen::Ref<Eigen::VectorXd>& one? If not, is there a workaround to modify an object passed as a const Eigen::Ref<const Eigen::VectorXd>&?

fdev
  • 127
  • 12
  • you can cast a constant reference to a non-constant reference, but modifying a const object is undefined behavior in any case. Why do you not change the signature to pass a non-const ref? – 463035818_is_not_an_ai Jul 05 '21 at 09:02
  • In my specific case, I must use a function that uses a non-const reference to a vector for efficiency reasons, and that ensures as a post-condition that the input vector doesn't get changed. Also, I'm sure that there's no undefined behavior because such function is used "internally", and I know that the variables I pass as const are actually non-const! – fdev Jul 05 '21 at 09:36
  • Anyway, I'm curious about an answer to my question, and I'm confident that my specific use case is the right one for using `const_cast`. – fdev Jul 05 '21 at 09:40

0 Answers0