A (somewhat cumbersome) solution is to give as_curvilinear_shape
the following signature:
const CurvilinearShape &as_curvilinear_shape(CurvilinearShape &storage) const;
If it's already the right shape, return *this
and leave storage
unchanged. If a conversion is needed, assign the conversion result to storage
and return a reference to storage
.
Or you might want to create a helper class and return it:
struct CurvilinearShapeOrRef{ CurvilinearShape storage; // or `std::optional`/`boost::optional`. const CurvilinearShape *ptr_override = nullptr; // C++98 can't do this, make a constructor. const CurvilinearShape &get() const {return ptr_override ? *ptr_override : storage;}};