c++ - Pointer casts for itk::SmartPointer? -
i'm looking std::static_pointer_cast
, std::const_pointer_cast
, , std::dynamic_pointer_cast
std::shared_pointer
.
i tried itk's documentation , itk::smartpointer
's source code , found nothing smart pointer casting.
in 1 particular case needed add 'constness' pointer (convert itk::smartpointer<t>
itk::smartpointer<const t>
) in order pass third-party function. passing raw pointer out of question because data deleted once automatically created const smart pointer goes out of scope.
the relatively safe solution found:
static_cast<itk::smartpointer<const t>>(itk_smart_pointer_of_t)
.
i don't know whether approach thread-safe or has other possible pitfalls. moreover, in case of dynamic_cast
things messier.
it seems strange itk doesn't have native std::const_pointer_cast
-like , other casts.
tl;dr: itk::smartpointer
doesn't need pointer casts, cast "raw" pointer , re-wrap instead.
itk smart pointers use intrusive reference counting, meaning owned object must provide reference counter.
itk::smartpointer
can used itk classes, or, more accurately, classes have register()
, unregister()
methods. example, descendants of itk::lightobject
class which, according documentation,
is highest level base class itk objects. implements reference counting ...
therefore, assumption that
passing raw pointer out of question because data deleted once automatically created const smart pointer goes out of scope.
is incorrect, because temporary itk::smartpointer
increase , decrease owned object's reference counter.
this means safe pass "not-so-raw" pointer function accepting smart pointer, or create temporary smart pointer manually , pass function.
Comments
Post a Comment