Const casting container value-types seems not possible. A comment in the other question suggests iterators as a solution, yet does not go into detail. Since I seemingly cannot simply convert a container from a non-const to a const version as a function parameter, I arrive at Iterators to maybe be able to do the job.
I actually have a vector<shared_ptr<Thing> >
to be treated as const vector<shared_ptr<Thing const> >
.
With it I intend to use the shared_ptr<Thing const>
as further references in other structures, without allowing those structures to alter the Thing
s. Those structures may create their own objects, stored by their own shared_ptr, if they want slightly different content within their containers, while still actively sharing most Things
with other objects.
So I would need either shared_ptr<const Thing>&
, or const shared_ptr<const Thing>&
from an Iterator through the sequence. Either would suffice, but just because one can be indifferent about passing references in this example, because of shared_ptr's copy semantics are about just that.
Yet even just using default const_iterator
, retrieved by cbegin()
,c.end()
and such, will give me a const shared_ptr<Thing>&
instead.
Edit: To copy the vector element for element would be one way technically, as in the other question, yet undesired for interface reasons. I am going for reinterpretation here, not copy.
Any suggestions on where a workaround might lie?