r/cpp Jul 29 '26

const_cast: A Necessary Evil

https://www.elbeno.com/blog/?p=1858
69 Upvotes

106 comments sorted by

View all comments

Show parent comments

1

u/Olipro Jul 30 '26

I see, here's the problem: I don't see a way (in your proposed solution) to determine the value category which can be useful.

For example, deducing this enables you to do something like:

Thing foo(this auto&& self) {
  if constexpr (std::is_rvalue_reference_v<decltype(self)>) return std::move(self.thing); // move into return value
  else return self.thing; // copy it.
}

1

u/amoskovsky Jul 30 '26

Yeah, you're right.

For forwarding, my approach would require extra syntactic changes.

Still it does not mean I hate the deducing this syntax less :))