owner / reference variant of proxy #233
SidneyCogdill
started this conversation in
Ideas
Replies: 1 comment 5 replies
-
it seems like And it still lacks an explicit owner proxy type (only constructable from |
Beta Was this translation helpful? Give feedback.
5 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
proxy is specifically designed such that it erases ownership (and custom allocator) information. both
std::make_unique<Impl>()
andImpl *
can be converted into aproxy<Interface>
and its resource lifetime will be managed correctly.This is powerful but not always intended. for example when I'm certain that my interface only wants to "observe" a state, under traditional sense it's a well established convention to use a
std::unique_ptr<State>
for owner, and pass the raw pointer obtained from.get()
express that it's only passing a non-owning view to that interface. ref: C++ Core GuidelinesWith proxy I don't think there is a way to distinguish owner from non-owning observer. Consider this example:
SetterGetter
can upcast intoSetter
. The interfacef
needs to set something on the object before storing it inside a vector. Passing a non-owning reference to a proxy polymorphic object can be done with&
+*
operators. The problem is whenf2
also accepts aproxy<F>
, this defeats local reasoning. the definition off2
may be invisible and only a declaration is visible (void f2(proxy<Setter>)
). With solely a function signature it's impossible to reason the intent off2
. It might want to take the ownership of the object (std::move
), or it might want to perform mutating operations (mutable reference). The implementer off2
is in a similar state: will the lifetime ofp
end as soon as the scope ends? can it be captured inside a coroutine promise and be used later? Aproxy<Setter>
tells nothing about it.It would be ideal if another two types of proxy are introduced that models
owner
andreference
:proxy
type.pro::make_proxy
.proxy
type. reference variant is different from the standardproxy
in the sense that its destructor is guaranteed to be trivial (doesn't do anything).This will not lose the benefits of proxy library, but allows those who want to better reason object lifetime to be able to explicitly state the ownership property. further more the owner / reference model aligns closely with the
[[gsl::Owner]]
/[[gsl::Pointer]]
attributes available in various toolchains, which means that static analyzers can now reason withproxy
's lifetime.It shouldn't be hard to implement either, as it can be implemented by:
proxy
type with slightly different constructors.pro::make_proxy
and friends which doesn't accept raw pointers or another instance of owner/proxy
, something likemake_unique_proxy
ormake_owning_proxy
.I would expect using them like this:
Beta Was this translation helpful? Give feedback.
All reactions