Is this the canonical way to create a std::experimental::observer_ptr
to a non-const
object that cannot mutate the value it points to?
auto i = int{0};
auto p = std::experimental::make_observer(&std::as_const(i));
*p = 1; // compilation error, as desired
EDIT:
What if the pointer already exists (which I suppose is the more common use-case)? Would we have to const_cast
?
auto i = int{0};
auto p = &i;
auto q = std::experimental::make_observer(const_cast<const int*>(p));
*q = 1; // compilation error, as desired