WIP: Make getiterationstep return shared ptr
The method IterativeSolver::getIterationStep should return a shared_ptr rather than a reference. Since IterationStep is an abstract base class, code that calls getIterationStep frequently does dynamic casting afterwards. You cannot do that with a reference though, unless using some trickery.
Unfortunately, simply changing the return type of the method is not backward compatible. This is why this merge request is marked as WIP. Any ideas on how to proceed?
Merge request reports
Activity
@graeser I don't understand you. When I take the address of a reference (as is currently done), then what I get is a C pointer, which I cannot use with dynamic_pointer_cast. I want shared_ptr as the return value of getIterationStep exactly because I want to apply dynamic_pointer_cast to it.
I understood @graeser's comment as an answer to the uncertainty whether dynamic casting the address can screw up the ownership, not as a solution to the current problem.
Btw. is stackobject_to_shared_ptr helping to avoid the ownership problem in your case? (it should at least in the case where the
IterativeSolver
lives longer than your own shared_ptr)@oliver.sander_at_tu-dresden.de You said that you're scared that plain
dynamic_cast
may screwup ownership. I only wanted to highlight, that it will screwup ownership if you store this in ashared_ptr
again.Regarding the original issue: The question if one should hand out a
shared_ptr
is IMO solely an ownership question. If you want to allow sharing owner ship to user code, then hand out theshared_ptr
. If the answer you don't want this, but pointer syntax, then return a raw pointer.If it's only about the RTTi syntax: I don't see why you cannot
dynamic_cast
a reference. You could even drop the assertions, because a faileddynamic_cast
on a reference will throw an exception (regardless of debug flags).BTW: Exporting
shared_ptr
s easily screws up const-correctness, because there's various interpretations on what this could mean. Eitherconst
means non-mutable ownership or non-mutable value. Classic const-correctness would be the former, semantically I'd opt for the latter. Since you export ashared_ptr
copy and no reference, I suspect you intend the latter which means you needshared_ptr<const T>
instead ofconst shared_ptr<T>
.added 64 commits
-
c7fa9269...8ecddd67 - 62 commits from branch
master
- 210ce85f - Make getIterationStep return std::shared_ptr instead of &
- 36fa3779 - getIterationStep returns a shared_ptr now
-
c7fa9269...8ecddd67 - 62 commits from branch