I have some existing code that in many places passes
boost::optional<std::shared_ptr<Data>>
as a parameter to a function.
The function itself does not need "ownership" of the function so copying the shared pointer is both inefficient and misleading. If it wasn't for the 'optional' I would change this to take a Data const& instead and deference the shared pointer in the call.
However I can't do this with optional as you can't have an optional reference.
I've seen alternative of just passing a raw pointer instead of a reference as it exactly fits the semantics required. However objections are sometimes raised to using raw pointers rightly or wrongly.
Is there a better way to pass this value other than just as a Data const * that ideally works in C++11 but any thoughts are required.
Closing and Linking to other answers are welcome if this is a duplicate, but the other answers I've seen tend to just say that optional<T&> is wrong or to use a T* without really talking about best style or alternatives.
Edit: Changed to boost::optional instead of std::optional as that is what is being used as the codebase is for the moment C++11