-
Notifications
You must be signed in to change notification settings - Fork 16
Add support for void AnySharedPointer #895
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Allow users to retrieve `std::shared_ptr<void>` directly from an `AnySharedPointer`.
| } | ||
|
|
||
| const std::shared_ptr<void>& as_void(void) const { return m_ptr; } | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks like a means of having an un-typed container for a pointer without losing type safety. I can understand wanting this to be able to write simpler template logic elsewhere. What confuses me here is throwing away your ability to guard type safe access by returning a shared_ptr<void>. Shouldn't callers need to provide the type of the underlying pointer so it can be checked before being allowed access to the shared pointer?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Quite correct. Actually, the main motivation to using AnySharedPointer is not necessarily that the code becomes any simpler, but that I can avoid using templated functions altogether and move to runtime versions of the same. These runtime versions are compiled once and don't increase build times downstream.
This particular routine is mainly intended to make unit tests a little easier to write in that sometimes I don't actually care about the underlying type, I just want whatever the pointed-to object is. There are implicit dangers of doing this--in particular:
std::shared_ptr<Derived> p;
AnySharedPointer x = std::static_pointer_cast<Base>(p);
ASSERT_EQ(p.get(), x.as_void()); // Might fail!But we tolerate this because it's not really used by anyone except autowiring, and that's the only intended customer for now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also when I use this type I get burned almost every time and have to rely pretty heavily on unit tests to ensure correctness. It's definitely not the friendliest thing in the world.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am thinking something like this?
template<class T>
std::shared_ptr<T> as_shared(void) const {
"Check that auto_id of T == m_ti"
return static_pointer_cast<T>(m_ptr);
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In fact, I have something like that now, it's called as
Allow users to retrieve
std::shared_ptr<void>directly from anAnySharedPointer.